[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

puts returns nil?

Arfon Smith

1/5/2007 9:30:00 AM

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

9 Answers

Jean Helou

1/5/2007 9:41:00 AM

0

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

Stefano Crocco

1/5/2007 9:47:00 AM

0

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

Carlos

1/5/2007 9:49:00 AM

0

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


Jörg Abelshauser

1/5/2007 9:53:00 AM

0

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

Arfon Smith

1/5/2007 10:05:00 AM

0

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

Gunner Asch

5/15/2013 1:59:00 AM

0

On Tue, 14 May 2013 20:47:26 -0500, " Gloom and doom .. the only NOISE
from the Party of No!" <doom@poo.com> wrote:

>On 5/14/2013 8:39 PM, Scout wrote:
>
>>> Contrary to what the Nazi Loons think, our president does not
>>> micromanage every second level manager in the entire
>>> US government.
>>
>> So you're telling us he is unable/unwilling to do the job for which he
>> was elected?
>>
>>
>>
>
>All this activity falls outside of the powers of the Executive office.
>
>It is not like he invaded a country claiming WMD and murdered a 1000000
>people for his dady's oil friends.
Who did that?

No one in the US. Some Saudi perhaps?


>
>
>
>
>--
>---
>
>http://4.bp.blogspot.com/_NUZ_fM-TQKQ/Sf9PJCiSbLI/AAAAAAAANYI/wzO53XgcCrM/s400/cr...
>
>SAVE AMERICA!!
>
>Spay or neuter
>your Republican!!

--
"You guess the truth hurts?

Really?

"Hurt" aint the word.

For Liberals, the truth is like salt to a slug.
Sunlight to a vampire.
Raid? to a cockroach.
Sheriff Brody to a shark
Bush to a Liberal

The truth doesn't just hurt. It's painful, like a red hot poker shoved
up their ass. Like sliding down a hundred foot razor blade using their
dick as a brake.

They HATE the truth."

Scout

5/15/2013 2:04:00 AM

0



" Gloom and doom .. the only NOISE from the Party of No!" <doom@poo.com>
wrote in message news:5192e92f$0$12753$bbae4d71@news.suddenlink.net...
> On 5/14/2013 8:39 PM, Scout wrote:
>
>>> Contrary to what the Nazi Loons think, our president does not
>>> micromanage every second level manager in the entire
>>> US government.
>>
>> So you're telling us he is unable/unwilling to do the job for which he
>> was elected?
>>
>>
>>
>
> All this activity falls outside of the powers of the Executive office.

CIC of the military falls outside of the power of the Executive office?

The State Department is outside the power of the Executive office?

Who else is responsible for consulate security?


deep

5/15/2013 3:40:00 AM

0

On Tue, 14 May 2013 22:04:11 -0400, "Scout"
<me4guns@verizon.removeme.this2.nospam.net> wrote:

>
>
>" Gloom and doom .. the only NOISE from the Party of No!" <doom@poo.com>
>wrote in message news:5192e92f$0$12753$bbae4d71@news.suddenlink.net...
>> On 5/14/2013 8:39 PM, Scout wrote:
>>
>>>> Contrary to what the Nazi Loons think, our president does not
>>>> micromanage every second level manager in the entire
>>>> US government.
>>>
>>> So you're telling us he is unable/unwilling to do the job for which he
>>> was elected?
>>>
>>>
>>>
>>
>> All this activity falls outside of the powers of the Executive office.
>
>CIC of the military falls outside of the power of the Executive office?
>
>The State Department is outside the power of the Executive office?
>
>Who else is responsible for consulate security?
>
Congress allocates the funds and Republicans voted to slash embassy
security. Look it up.

Scout

5/15/2013 4:02:00 AM

0



"deep" wrote in message news:lr06p8l8fkcd9at21rt6dp361i6kq02q4u@4ax.com...
> On Tue, 14 May 2013 22:04:11 -0400, "Scout"
> <me4guns@verizon.removeme.this2.nospam.net> wrote:
>
>>
>>
>>" Gloom and doom .. the only NOISE from the Party of No!" <doom@poo.com>
>>wrote in message news:5192e92f$0$12753$bbae4d71@news.suddenlink.net...
>>> On 5/14/2013 8:39 PM, Scout wrote:
>>>
>>>>> Contrary to what the Nazi Loons think, our president does not
>>>>> micromanage every second level manager in the entire
>>>>> US government.
>>>>
>>>> So you're telling us he is unable/unwilling to do the job for which he
>>>> was elected?
>>>>
>>>>
>>>>
>>>
>>> All this activity falls outside of the powers of the Executive office.
>>
>>CIC of the military falls outside of the power of the Executive office?
>>
>>The State Department is outside the power of the Executive office?
>>
>>Who else is responsible for consulate security?
>>
> Congress allocates the funds and Republicans voted to slash embassy
> security. Look it up.

However, that doesn't mean they prevented additional security for Benghazi.

That is a decision made by the Executive branch over which Obama has power.

You don't suppose he could transfer a person from an embassy where there
isn't a current major security threat to where there are.
1 here, 1 there, 1 from over there, and eventually you would have enough
personal to provide the security needed at Benghazi.

Or, he could always order the military to provide the additional security,
and SOB they are paid by a different allocation.

Na, it wasn't an issue of money, but rather a misallocation of the funds
they did have and that is ALL on Obama's plate.