Hello.

I am trying to deal with simple ruby ​​constructions. I get an array from json data:

response = JSON.parse(RESPONSE) 

I try to process it this way:

 class GetFilm attr_accessor :group_theaters :parameter def initialize(*catalogue) title = self.get_data(catalogue, "film") @group_theaters = title end def get_data(*catalogue, parameter) @array = catalogue.length end end 

Sending data:

 s = GetFilm.new(response.values.to_a) puts s.group_theaters 

For some reason, I always have catalogue.length 1, initially json looks like:

 '{"catalog": { "1": { "title": "Left Behind", "theaters": ["Ukraine", "Big World"], }, "2": { "title": "Into the storm", "theaters": ["Ukraine", "Big World"], } }}' 

Update

 RESPONSE1 = '{"catalog": { "1": { "title": "Left Behind", "theaters": ["Ukraine", "Big World"] }, "2": { "title": "Into the storm", "theaters": ["Ukraine", "Big World"] } }}' response = JSON.parse(RESPONSE1) s = GetFilm.new(response.values) 

And what happened: 1

puts catalogue in the get_data method shows an array in curly braces:

 {"1"=>{"title"=>"Left Behind", "theaters"=>["Ukraine", "Big World"]}, "2"=>{"title"=>"Into the storm", "theaters"=>["Ukraine", "Big World"]}} 
  • one
    It is a sin to cut indents more than two spaces. - Vladimir Gordeev

1 answer 1

Remove the asterisk from the parameters, for catalogue.length seems to you the number of parameters passed to the method.

Read more: http://www.tutorialspoint.com/ruby/ruby_methods.htm
See the Variable Number of Parameters.

If you decide that the asterisk is a parameter passing by reference, then you are mistaken.

  • I decided that the asterisk is an array declaration. Removed - nothing has changed. - tiktock
  • Give the resulting code. And what is passed to the method. - Mayar
  • Updated the question - tiktock
  • one
    JSON.parse (RESPONSE1) returns a one-element hash. Accordingly .length it seems. - mayar