Puts returns nil?

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

--
Posted via http://www.ruby-forum.com/.

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

···

On 1/5/07, Arfon Smith <arfon.smith@gmail.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

Alle 10:30, venerdì 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 = 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_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

I hope this helps

Stefano

Arfon Smith wrote:
> [...]

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.

···

--

Arfon Smith wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

Jörg Abelshauser wrote:

Arfon Smith wrote:

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

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

···

--
Posted via http://www.ruby-forum.com/\.