0

我目前正在从事一个基本的 Ruby 编程项目,该项目专注于创建类以及对这些类进行操作。我的经验很少,但了解 Ruby 的大致思路。

我的任务是制作一个 Array2 类。从类中创建数组,对数组执行操作。我尝试的方法是 to-string 方法和具有两个数组参数的 is-reverse 方法,并测试第一个数组是否与第二个数组相反。

这是我的尝试,我尝试过,但无法将数组正确传递到类中。我也相信我有一些呼叫并发症。

class Array2
def initialize (a)
    @array = Array.new(a)
end

def to_s
    return @array
end

def isreverse (array1,array2)
    reverasea = Array.new
    reverasea = array1.reverse
    if  (reversea = array2) then
        return "The First Array Is The Reverse Of The Second Array"
    else
        return "The First Array Is Not The Reverse Of The Second Array"
    end
end
end

array1 = ["4","5","6","7"]
array2 = ["7","6","5","3"]

a1 = Array2.new(array1)
a2 = Array2.new(array2)

puts a1.to_s
puts a2.to_s
puts a1.isreverse(array1, array2)
4

3 回答 3

1

你有一个任务,你可能意味着平等测试:

 if  (reversea = array2) then

您可以完全放弃 reversea 并进行测试(这需要使用 reverse 方法Array2

if (array1.reverse == @array) then

我个人会将 isreverse 设为布尔值,并消除再次传入同一数组的需要:

def isreverse? (array1)
    return (@array.reverse == array1)
end

然后像使用它

puts "The First Array Is#{a1.isreverse?(a2)?"":" Not"} The Reverse Of The Second Array"

把它们放在一起,它看起来像:

class Array2
    def initialize (a)
        @array = Array.new(a)
    end

    def to_s
        return @array
    end

    def reverse
        @array.reverse
    end

    def isreverse? (array1)
        return (array1.reverse == @array)
    end
end

array1 = ["4","5","6","7"]
array2 = ["7","6","5","3"]

a1 = Array2.new(array1)
a2 = Array2.new(array2)

puts a1.to_s
puts a2.to_s
puts "The First Array Is#{a1.isreverse?(a2)?"":" Not"} The Reverse Of The Second Array"

小提琴

于 2013-08-10T01:17:14.493 回答
1

以下是对您现有方法的一些调整。我在更改原始内容的地方发表了评论::

class Array2
  def initialize (a)
    @array = Array.new(a)
  end

  def to_array   # to_s is a misnomer: it doesn't return a string
    return @array
  end

  def isreverse (a)
    #reverasea = Array.new  NOTE this is not needed; the following .reverse creates a new array for you
    reversea = a.to_array.reverse  # get the reverse of the array represented

    # NOTE = is assign, == is compare in this case
    # The following compares the reversed of the array `a` with the array value of this class, @array
    if  (reversea == @array) then
      return "The First Array Is The Reverse Of The Second Array"
    else
      return "The First Array Is Not The Reverse Of The Second Array"
    end
  end
end

array1 = ["4","5","6","7"]
array2 = ["7","6","5","3"]

a1 = Array2.new(array1)
a2 = Array2.new(array2)

puts a1.to_array  # (renamed)
puts a2.to_array  # (renamed)
#puts a1.isreverse(array1, array2)  NOTE you don't need to pass array1 into class a1 since it is already made from array1
puts a1.isreverse(a2)
于 2013-08-10T01:27:56.020 回答
1

我会选择更简单的东西,例如:

Filename: reverser.rb

class Reverser
  def self.is_reverse_of(array1,array2)
    array1_reversed=array1.reverse
    is_or_isnt= (array1_reversed==array2)? 'Not ' : ''
    return "The First Array Is #{is_or_isnt}The Reverse Of The Second Array"
  end
end
puts Reverser.is_reverse_of(["4","5","6","7"], ["7","6","5","4"])
puts Reverser.is_reverse_of(["4","5","6","7"], ["7","6","5","3"])

ruby reverser.rb
The First Array Is Not The Reverse Of The Second Array
The First Array Is The Reverse Of The Second Array

这个想法是使用类级别的方法而不是实例化太多并且代码更少。

于 2013-08-10T01:29:02.410 回答