puts returns nil?

A

Arfon Smith

Hi, I'm sorry to be asking such a daft question but I need a concept
explaining....


I'm working through the excellent Chris Pine 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 + '.'

Now the output from this is:

mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
yellow submarine

-------------

OK, so here's the problem, I _don't_ get why it doesn't return the
following:


mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
mooooooo...mooooooo...mooooooo
yellow submarine

-------------

Why do we get one set of 'moos' but not a second?

Sorry for such a newbiee question.

Cheers

arfon
 
J

Jean Helou

because you only call say_moo once.

x = say_moo 3

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
 
S

Stefano Crocco

Alle 10:30, venerd=C3=AC 5 gennaio 2007, Arfon Smith ha scritto:
Hi, I'm sorry to be asking such a daft question but I need a concept
explaining....


I'm working through the excellent Chris Pine 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 =3D say_moo 3
puts x.capitalize + ', dude...'
puts x + '.'

Now the output from this is:

mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
yellow submarine

-------------

OK, so here's the problem, I _don't_ get why it doesn't return the
following:


mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
mooooooo...mooooooo...mooooooo
yellow submarine

-------------

Why do we get one set of 'moos' but not a second?

Sorry for such a newbiee question.

Cheers

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_mo=
o's=20
return value, which is determinated by the method's last line: 'yellow=20
submarine'.=20

Writing x=3Dsay_moo 3 will put in x only the string 'yellow submarine', bec=
ause=20
this is what say_moo returns. In other words, the line=20
mooooooo...mooooooo...mooooooo is written by a side effect of say_moo; the=
=20
other two lines are produced by your own puts statements.

To get the output you expected (well, not exactly, captialize would work a=
=20
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=
=20
would return the string
"mooooooo...mooooooo...mooooooo
yellow submarine"
which would be written twice by your puts statements

I hope this helps

Stefano
 
C

Carlos

Arfon said:

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...'.
puts x + '.'

Puts the value of x, plus a dot.

HTH. Good luck.
--
 
J

Jörg Abelshauser

Arfon said:
Hi, I'm sorry to be asking such a daft question but I need a concept
explaining....


I'm working through the excellent Chris Pine 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 + '.'

Now the output from this is:

mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
yellow submarine

-------------

OK, so here's the problem, I _don't_ get why it doesn't return the
following:


mooooooo...mooooooo...mooooooo
Yellow submarine, dude...
mooooooo...mooooooo...mooooooo
yellow submarine

-------------

Why do we get one set of 'moos' but not a second?

Sorry for such a newbiee question.

Cheers

arfon

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:

puts 'mooooooo...' * 3 + 'yellow submarine'
mooooooo...mooooooo...mooooooo...yellow submarine

best regards Jörg
 
A

Arfon Smith

Jörg Abelshauser said:
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:

puts 'mooooooo...' * 3 + 'yellow submarine'
mooooooo...mooooooo...mooooooo...yellow submarine

best regards Jörg


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....

Right?!

Cheers
arfon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top