2

I'm still new in Ruby on Rails. Today I'm trying to write some codes which can run the the following:

image = Image.new([
  [0, 0, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 1],
  [0, 0, 0, 0]
])

image.output_image

And I'm having trouble setup the initialize. My codes is as below, can someone help me? Thanks a lot

class Subary
    attr_accessor :num1, :num2, :num3, :num4

    def initialize (num1, num2, num3, num4)
        self.num1 = num1
        self.num2 = num2
        self.num3 = num3
        self.num4 = num4
    end

    def output_subary
        puts "#{num1}#{num2}#{num3}#{num4}"
    end

end

# subary = Subary.new(0,0,0,0)
# puts subary.output_subary

class Image
    def initialize 
        @subarys = []
        @subarys << Subary.new(:num1, :num2, :num3, :num4)
        @subarys << Subary.new(:num1, :num2, :num3, :num4)
        @subarys << Subary.new(:num1, :num2, :num3, :num4)
        @subarys << Subary.new(:num1, :num2, :num3, :num4)
    end

    def output_image
        @subarys.each do |list|
            list.output_subary
        end
    end
end

image = Image.new([
  [0, 0, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 1],
  [0, 0, 0, 0]
])
image.output_image
3
  • Ruby does not have a concept of "multidimensional arrays". The term is used loosely to describe an array having a particular structure, but it can be confusing and is best to avoid. All arrays merely contain objects, which are normally referred to "elements". Your array image contains four elements, each of which is an array of four elements. Had it been what you might think of as a "three-dimensional" array, it might contain four elements that are arrays, each of which is an array of four elements that are themselves arrays of, say, three elements. Commented Nov 24, 2015 at 19:25
  • Thanks for your reply Cary. Help me get more clear about the concept. Commented Nov 24, 2015 at 19:39
  • ..and this is a perfectly-good array: [1, 3.1, "cat", :dog, 4..7, [5, {3=>{:a=>[4,5]}}]]. Commented Nov 24, 2015 at 19:44

1 Answer 1

1
`initialize': wrong number of arguments (1 for 0)

This error means that, initialize method does not take any argument (0), but you passed one argument to it. Change the initialize method's definition in your Image class. Then, it should work.

class Subary
    attr_accessor :num1, :num2, :num3, :num4

    def initialize(sub_array)
        self.num1 = sub_array[0]
        self.num2 = sub_array[1]
        self.num3 = sub_array[2]
        self.num4 = sub_array[3]
    end

    def output_subary
        puts "#{num1}#{num2}#{num3}#{num4}"
    end
end

# subary = Subary.new(0,0,0,0)
# puts subary.output_subary

class Image
    def initialize(array_of_arrays)
        @subarys = []
        @subarys << Subary.new(array_of_arrays[0])
        @subarys << Subary.new(array_of_arrays[1])
        @subarys << Subary.new(array_of_arrays[2])
        @subarys << Subary.new(array_of_arrays[3])
    end

    def output_image
        @subarys.each do |list|
            list.output_subary
        end
    end
end

image = Image.new([
  [0, 0, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 1],
  [0, 0, 0, 0]
])
image.output_image
# => 0000
# => 0100
# => 0001
# => 0000
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for reply. Actually I'm confused in what I could put inside the argument. As my final out put wann be: 0000 0100 0010 0000 which are four arrays.
ok, give me some time. I will update my answer to met your requirement.
Ken, the error message also gave the line where the exception was raised (def initialize). You need to get in the habit of studying error messages carefully. They contain valuable information, often pinpointing the problem, as here.
That's so cool!Thanks K M. I wasn't know that "array" could simply put inside the argument. I just took a lot of time figure our how it should be made to represent four arrays.
Thank you @KMRakibulIslam, that make sense for me now.
|

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.