Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3576 > unrolled thread
| Started by | Fily Salas <fs_tigre@hotmail.com> |
|---|---|
| First post | 2011-04-27 15:25 -0500 |
| Last post | 2011-04-27 18:38 -0500 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.ruby
Help me understand this SIMPLE Ruby code, using puts in a def Fily Salas <fs_tigre@hotmail.com> - 2011-04-27 15:25 -0500
Re: Help me understand this SIMPLE Ruby code, using puts in a def Sam Duncan <sduncan@wetafx.co.nz> - 2011-04-27 15:35 -0500
Re: Help me understand this SIMPLE Ruby code, using puts in a def Roger Braun <roger@rogerbraun.net> - 2011-04-27 15:41 -0500
Re: Help me understand this SIMPLE Ruby code, using puts in a def Cee Joe <cyril_jose@ymail.com> - 2011-04-27 15:47 -0500
Re: Help me understand this SIMPLE Ruby code, using puts in a def Fily Salas <fs_tigre@hotmail.com> - 2011-04-27 18:38 -0500
| From | Fily Salas <fs_tigre@hotmail.com> |
|---|---|
| Date | 2011-04-27 15:25 -0500 |
| Subject | Help me understand this SIMPLE Ruby code, using puts in a def |
| Message-ID | <bde5d72cb289acdd7b880c6c83869049@ruby-forum.com> |
Hi, I was practicing ruby and accidentally came across something that got me thinking (nothing important but I want to know why). Why does the example-1 works and not example-2, if all I'm doing is moving the puts? Example-1: def message "this is working" end puts message.upcase Example-2: def message puts "this is working" end message.upcase Can someone explain this a little bit? I could just ignore it but I would like to know the reason. Thanks -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Sam Duncan <sduncan@wetafx.co.nz> |
|---|---|
| Date | 2011-04-27 15:35 -0500 |
| Message-ID | <4DB87E03.3070601@wetafx.co.nz> |
| In reply to | #3576 |
This is where irb is handy. The short of it is that in your first example a string is returned on which you call upcase. In your second example the thing returned is the result of puts, which is nil. > irb ruby-1.9.2-p0 > foo = puts 'foo' foo => nil ruby-1.9.2-p0 > foo => nil ruby-1.9.2-p0 > foo.upcase NoMethodError: undefined method `upcase' for nil:NilClass Does that help? Sam On 28/04/11 08:25, Fily Salas wrote: > Hi, > > I was practicing ruby and accidentally came across something that got me > thinking (nothing important but I want to know why). > > Why does the example-1 works and not example-2, if all I'm doing is > moving the puts? > > Example-1: > def message > "this is working" > end > puts message.upcase > > > Example-2: > def message > puts "this is working" > end > message.upcase > > Can someone explain this a little bit? I could just ignore it but I > would like to know the reason. > > Thanks >
[toc] | [prev] | [next] | [standalone]
| From | Roger Braun <roger@rogerbraun.net> |
|---|---|
| Date | 2011-04-27 15:41 -0500 |
| Message-ID | <BANLkTinNr-zCJWWfW4zjdoak4PB3pFa+GQ@mail.gmail.com> |
| In reply to | #3576 |
Hi Fily 2011/4/27 Fily Salas <fs_tigre@hotmail.com>: > Hi, > > I was practicing ruby and accidentally came across something that got me > thinking (nothing important but I want to know why). > > Why does the example-1 works and not example-2, if all I'm doing is > moving the puts? > > Example-1: > def message > "this is working" > end > puts message.upcase > > > Example-2: > def message > puts "this is working" > end > message.upcase > > Can someone explain this a little bit? I could just ignore it but I > would like to know the reason. The return value of the "puts" method is always nil, not the string it prints. What your second example does is try to invoke the "upcase" method on nil. -- Roger Braun rbraun.net | humoralpathologie.de
[toc] | [prev] | [next] | [standalone]
| From | Cee Joe <cyril_jose@ymail.com> |
|---|---|
| Date | 2011-04-27 15:47 -0500 |
| Message-ID | <0d5b885d71f3b1d93f93f2b42090d5f4@ruby-forum.com> |
| In reply to | #3576 |
I agree with Sam: >> def message >> "this is working" >> end => nil >> puts message.upcase THIS IS WORKING => nil >> def message1 >> puts "this is working" >> end => nil >> puts message1 this is working nil <----- extra nil => nil Since the nil is the last thing that is read in the method, upcase is called on that nil and upcase throws an error b/c you can't use that method on a nil. Make sense? -Cee -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Fily Salas <fs_tigre@hotmail.com> |
|---|---|
| Date | 2011-04-27 18:38 -0500 |
| Message-ID | <81483a1b63747f6daec1272b959aad63@ruby-forum.com> |
| In reply to | #3576 |
Got it. Thank you all very much for your help! -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web