0

I am currently working on a basic Ruby programming project, that focuses on creating classes, and operations on those classes. I have very little experience, but understand the general idea of Ruby.

My task is making an Array2 class. Creating arrays from the class, perform operations on the arrays. The methods I attempted are a to-string method, and a is-reverse method that has two array parameters, and tests if the first array is the reverse of the second array.

Here is my attempt, I tried but I am having trouble passing the arrays properly into the class. Also I believe that I am having some calling complications.

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)
0

3 Answers 3

1

You have an assignment where you probably meant equality test:

 if  (reversea = array2) then

you could dispense with reversea entirely and just test (this requires a reverse method in Array2)

if (array1.reverse == @array) then

I would personally make isreverse a boolean, and eliminate the need to pass in the same array again:

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

then use it like

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

put it all together and it looks like:

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"

fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Here are some adjustments to your existing approach. I put in comments where I changed the original::

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)

Comments

1

I would go for something simpler such as:

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

The idea being to use a class level method and not instantiate as much and have less code.

1 Comment

Alright the code seems a lot more straightforward, and I can see how everything was passed. Thanks alot for you help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.