Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“can't convert Symbol into Integer” weird error

Tags:

ruby

haml

The is the hash I am working on,

a = {
  #...
  :fares => {
    :itinerary_fare => {
      :segment_names=>"C", 
      :free_seats => "6", 
      :fare_for_one_passenger => {
        :free_seats=>"0", 
        :@currency => "TL", 
        :@non_refundable => "false", 
        :@price => "439.0", 
        :@service_fee => "25.0", 
        :@tax => "33.0", 
        :@type => "Y"
      },
      :@currency => "TL", 
      :@non_refundable => "false", 
      :@price => "439.0", 
      :@service_fee => "25.0", 
      :@tax => "33.0", 
      :@type => "C"
    },
    :@currency => "TL", 
    :@tax => "33.0"
  }, 
  #..
}

also here another example http://pastebin.com/ukTu8GaG.

The code that gives me headhaches,

a[:fares][:itinerary_fare].each do |f|
   puts f[:@price]
end

If I write this into console, it gives me "can't convert Symbol into Integer" error. But if I write, a[:fares][:itinerary_fare][:@price] it works pretty fine.

The weirdest part is, if I write the code to a haml file

%tbody
    [email protected] do |a|
     %tr.flight
      %td
       -a[:fares][:itinerary_fare].each do |f|
        -puts f[:@price] #Weird stuff happens here
        .prices
         %input{:type=>"radio",:name=>"selectedfight",:value=>"#{a[:id]}"}
          = f[:@price]
         %br

It works, it prints the prices to my console, yet it fails at the SAME LINE.

can't convert Symbol into Integer file: flights.haml location: [] line: 18

This is the most disturbing error I have ever seen, thanks for any help.

Most of the time there are more than 1 :itinerary_fare, I have to iterate.

My data can be shown as http://postimage.org/image/6nnbk9l35/

like image 419
gkaykck Avatar asked May 10 '12 17:05

gkaykck


1 Answers

a[:fares][:itinerary_fare] is a Hash. Hash#each yields key-value-pair arrays to the block.

So, f takes e.g. the array [:@price, "439.0"] in the block.

Hence you are using a symbol (:@price) as an array index. An integer is expected.

In a[:fares][:itinerary_fare][:@price] you are giving it as hash key which works of course.

like image 143
undur_gongor Avatar answered Nov 07 '22 01:11

undur_gongor