Created
August 22, 2015 01:29
-
-
Save eternal44/8b4cf82eb5cfb9f93b82 to your computer and use it in GitHub Desktop.
A quick demo of the difference between puts & return for calling methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def addstring(x,y) | |
z = x + y | |
return z | |
end | |
def putstring(x,y) | |
z = x + y | |
puts z | |
end | |
########## | |
# OUTPUT # | |
########## | |
>> words = addstring(addstring("Have ","Fun "),"Today") | |
=> "Have Fun Today" | |
>> words = putstring(addstring("Have ","Fun "),"Today") # notice putting a return call on a method still works | |
Have Fun Today | |
=> nil | |
>> words = putstring(putstring("Have ","Fun "),"Today") | |
Have Fun | |
NoMethodError: undefined method `+' for nil:NilClass | |
from /home/james/lab/scratch/puts_vs_return.rb:7:in `putstring' | |
from (irb):11 | |
from /home/james/.rbenv/versions/2.2.0/bin/irb:11:in `<main>' | |
>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment