ruby - Printing the return value -


i copied , ran following code taken book programming in ruby: pragmatic programmer's guide:

a = [ 1, 'cat', 3.14 ] a[0] 

the book says program supposed give 1, doesn't, gives nothing. then, tried next exercise:

a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ] a[0] a[3] 

which supposed return "ant" , "dog", still, nothing. can tell me screwing up?

try entering these lines irb session instead. you'll see promised results. if want run file, have different.

a[0] 

this evaluate expression. irb show value, running file nothing. must explicitly print standard output. this:

puts a[0] 

update

here's example irb session

% irb 1.9.3p125 :001 > = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]  => ["ant", "bee", "cat", "dog", "elk"] 1.9.3p125 :002 > a[0]  => "ant" 1.9.3p125 :003 > a[3]  => "dog" 

i suggest stick irb. better learning because of interactivity.


Comments