YoniGeek
YoniGeek

Reputation: 4093

Search algorithm is not working

I want to search a given number in this array filled with hashes:

Time_tables = [
  { name: 01251},
  { name: 05012},
  { name: 03232},
  { name: 02435},
  { name: 04545},
  { name: 03545}
]

My expected result is:

{name: 02435}

This is the code:

def finding_numbers(tables, train_number)
  tables.each do |i|
    if i[:name] == train_number
      p i
    end
  end
end

finding_numbers(Time_tables, 02435)

If I run this code, I get this:

{:name=>1309}

This number is not even in the array. What is going on?

Upvotes: 0

Views: 44

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

In ruby number literals starting with 0 are read as an octal representation:

puts 02435
# => 1309

So actually your code runs fine: it finds the correct element, it only writes the number decimally, and not octally

Upvotes: 4

Related Questions