Hi, I’m sorry to be asking such a daft question but I need a concept
explaining…
I’m working through the excellent Chris P. book ‘Learn to program’ and
I don’t get one of the examples he gives. In particular I don’t follow
the following example:
def say_moo number_of_moos
puts ‘mooooooo…’ * number_of_moos
‘yellow submarine’
end
x = say_moo 3
puts x.capitalize + ‘, dude…’
puts x + ‘.’
Hi, I’m sorry to be asking such a daft question but I need a concept
explaining…
I’m working through the excellent Chris P. book ‘Learn to program’ and
I don’t get one of the examples he gives. In particular I don’t follow
the following example:
def say_moo number_of_moos
puts ‘mooooooo…’ * number_of_moos
‘yellow submarine’
end
x = say_moo 3
puts x.capitalize + ‘, dude…’
puts x + ‘.’
Hi Arfon
in every ruby-function, the last statement (here ‘yellow submarine’) is
the return-value of this function, nothing else !
Further yes, puts returns nil.
puts(obj, …) => nil
You can see the declaration of all ruby-functions in the “fxri”,
installed with your ruby-interpreter. It’s very helpful to work with it.
3. If you intention was to append the ‘yellow submarine’ to the puts 1
line above, you need an ‘+’ to do this:
Hi, I’m sorry to be asking such a daft question but I need a concept
explaining…
I’m working through the excellent Chris P. book ‘Learn to program’ and
I don’t get one of the examples he gives. In particular I don’t follow
the following example:
def say_moo number_of_moos
puts ‘mooooooo…’ * number_of_moos
‘yellow submarine’
end
x = say_moo 3
puts x.capitalize + ‘, dude…’
puts x + ‘.’
Hi Arfon
in every ruby-function, the last statement (here ‘yellow submarine’) is
the return-value of this function, nothing else !
Further yes, puts returns nil.
puts(obj, …) => nil
You can see the declaration of all ruby-functions in the “fxri”,
installed with your ruby-interpreter. It’s very helpful to work with it.
3. If you intention was to append the ‘yellow submarine’ to the puts 1
line above, you need an ‘+’ to do this:
Ahh, OK, thanks for this. I think I was confused as I thought the first
‘puts’, i.e.
puts x.capitalize + ‘, dude…’
was producing the moos and the second one (puts x + ‘.’) wasn’t. Both
of the puts are just returning the last statement, it’s the ‘x= say_moo
3’ that is giving the moooo…mooooo…moooooo…
A method returns the value of the last statement, not a list of all of
them. In your method, the return value of puts (which is nil, btw) is
discarded.
def say_moo number_of_moos
puts ‘mooooooo…’ * number_of_moos
‘yellow submarine’
end
x = say_moo 3
Here you call say_moo. Say_moo puts 3 moo’s in you screen, then returns
‘yellow submarine’. This return value is assigned to x.
puts x.capitalize + ‘, dude…’
Puts the value of x (‘yellow submarine’) capitalized, plus ‘, dude…’.
Alle 10:30, venerdì 5 gennaio 2007, Arfon S. ha scritto:
puts ‘mooooooo…’ * number_of_moos
Yellow submarine, dude…
mooooooo…mooooooo…mooooooo
arfon
say_moo does two separate things:
1- writes the string ‘mooooooo…’ on the screen number_of_moos times
2- returns the string ‘yellow submarine’
The call to puts in the definition of say_moo has nothing to do with
say_moo’s
return value, which is determinated by the method’s last line: ‘yellow
submarine’.
Writing x=say_moo 3 will put in x only the string ‘yellow submarine’,
because
this is what say_moo returns. In other words, the line
mooooooo…mooooooo…mooooooo is written by a side effect of say_moo;
the
other two lines are produced by your own puts statements.
To get the output you expected (well, not exactly, captialize would work
a
little differently), say_moo should have defined like this:
def say_moo number_of_moos
mooooooo…’ * number_of_moos+"\nyellow submarine"
end
In this case, say_moo wouldn’t write nothing on the screen by itself,
but
would return the string
“mooooooo…mooooooo…mooooooo
yellow submarine”
which would be written twice by your puts statements
the interpreter will resolve and execute the method call and will
store the result in x thus say_moo is called once and x is set to
the string “Yellow submarine”
puts x
this will only print the content of x which is a string and won’t call
say_moo another time
did it help ?
jean
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.