Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2818 > unrolled thread
| Started by | Nathan McDorman <nmcdorman@gmail.com> |
|---|---|
| First post | 2011-04-13 23:27 -0500 |
| Last post | 2011-04-14 00:55 -0500 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.ruby
Noob question re:returning nil Nathan McDorman <nmcdorman@gmail.com> - 2011-04-13 23:27 -0500
Re: Noob question re:returning nil Vincent Manis <vmanis@telus.net> - 2011-04-14 00:08 -0500
Noob question re:returning nil Nathan McDorman <nmcdorman@gmail.com> - 2011-04-14 00:55 -0500
| From | Nathan McDorman <nmcdorman@gmail.com> |
|---|---|
| Date | 2011-04-13 23:27 -0500 |
| Subject | Noob question re:returning nil |
| Message-ID | <72f3e8defb9cb5e5475d2089568692b5@ruby-forum.com> |
Hello all, I am a beginner programmer and a beginner to Ruby. I was just going through a tutorial book and trying to create some programs. The current program I am working on I am having trouble with. I have defined a method and the last thing in the method is a puts concatenation. The program runs and does everything correctly except for when I call the method it puts the string and then nil underneath. I thought the return value was the last value of the string so I don't understand why it is doing this. How do I get rid of the nil? -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Vincent Manis <vmanis@telus.net> |
|---|---|
| Date | 2011-04-14 00:08 -0500 |
| Message-ID | <13E58820-CE18-4095-AB6A-86FB495597B5@telus.net> |
| In reply to | #2818 |
On 2011-04-13, at 21:27, Nathan McDorman wrote:
> Hello all, I am a beginner programmer and a beginner to Ruby. I was just
> going through a tutorial book and trying to create some programs. The
> current program I am working on I am having trouble with.
>
> I have defined a method and the last thing in the method is a puts
> concatenation. The program runs and does everything correctly except for
> when I call the method it puts the string and then nil underneath.
>
> I thought the return value was the last value of the string so I don't
> understand why it is doing this. How do I get rid of the nil?
If I'm not mistaken, you're doing something like this.
irb(main):001:0> def myputs(x)
irb(main):002:1> puts("**" + x + "**")
irb(main):003:1> end
=> nil
irb(main):004:0> myputs("foobar")
**foobar**
=> nil
The return value of myputs is the value of the last expression, which is whatever puts returns. If you do ri 'Kernel#puts', you will find that it always returns nil, which is what you're getting. That's a characteristic of Kernel#puts. The other piece to the puzzle is that irb displays the return value of whatever you enter. So calling myputs displays a message, and then shows the return value of nil.
There may be a way of turning off the display of returned values in irb. If so, I don't know what it is, because I've never wanted to use it. Maybe someone else can tell you that.
The important point is that displaying the returned value is only a characteristic of irb. Running a program via the ruby command doesn't display return values, and therefore you only get printed that which you explicitly specify is to be printed, via puts or print or some such.
Hope that helps -- vincent
[toc] | [prev] | [next] | [standalone]
| From | Nathan McDorman <nmcdorman@gmail.com> |
|---|---|
| Date | 2011-04-14 00:55 -0500 |
| Message-ID | <6cf2537166557d9ca4643e40603bb38e@ruby-forum.com> |
| In reply to | #2818 |
Thank you guys so much I really appreciate the help! I've changed the program to a method that just calculates what I want it to return and holds it in a var placed at the end of the method. Then I call puts outside of the method on the method. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web