tags:

views:

64

answers:

3
Q: 

Ruby Array

EDIT : First part removed i found the answer.

I have the following problem : im reading a number from the input and trying to use that number to access a given array position. I get the following results

value #=> "0"
value.to_i #=> 0
myArray[0] #=> MyObject
myArray[value.to_i] #=> nil
+3  A: 

That works for me.

irb(main):012:0> myArray = ['first']
=> ["first"]
irb(main):013:0> value = '0'
=> "0"
irb(main):014:0> myArray[value.to_i]
=> "first"
jshen
A: 

Try myArray[Integer(value)] (although value.to_i works for me as well):

>> value = "0"
=> "0"
>> myArray = ["a", "b"]
=> ["a", "b"]
>> myArray[0]
=> "a"
>> myArray[Integer(value)]
=> "a"
Chris Bunch
A: 

Thank you for all your replies, i was trying to access and array inside a class variable with:

@myclass.myarray[value]

when i made a getter method and accessed myarray inside the object the error disappeared. If anyone can explain this behavior please let me know

Nuno Furtado
You need to show more code that duplicates the problem
jshen