ruby - Create An Array Class With Certain Operation To Implement -


i working on basic ruby programming project, focuses on creating classes, , operations on classes. have little experience, understand general idea of ruby.

my task making array2 class. creating arrays class, perform operations on arrays. methods attempted to-string method, , is-reverse method has 2 array parameters, , tests if first array reverse of second array.

here attempt, tried having trouble passing arrays class. believe having 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)         return "the first array reverse of second array"     else         return "the first array not reverse of 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) 

you have assignment meant equality test:

 if  (reversea = array2) 

you dispense reversea entirely , test (this requires reverse method in array2)

if (array1.reverse == @array) 

i make isreverse boolean, , eliminate need pass in same array again:

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

then use like

puts "the first array is#{a1.isreverse?(a2)?"":" not"} reverse of second array" 

put , 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"} reverse of second array" 

fiddle


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -