Skip to content

Instantly share code, notes, and snippets.

@eternal44
Created August 22, 2015 01:29
Show Gist options
  • Save eternal44/8b4cf82eb5cfb9f93b82 to your computer and use it in GitHub Desktop.
Save eternal44/8b4cf82eb5cfb9f93b82 to your computer and use it in GitHub Desktop.
A quick demo of the difference between puts & return for calling methods
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