Created
March 31, 2010 15:30
-
-
Save mikeymicrophone/350469 to your computer and use it in GitHub Desktop.
This file contains 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
# to start at the beginning and walk through the revisions, use these links ------------------- | |
#| | |
class Slapper #| | |
def slap #| | |
before_slap #|----> | |
puts "I just slapped something" | |
end | |
def before_slap | |
end | |
def Slapper.local_methods | |
puts (new.methods - Object.new.methods) | |
end | |
end | |
class Mom < Slapper | |
attr_accessor :slap_phrase | |
def initialize | |
self.slap_phrase = 'Ki-ya!' | |
end | |
def before_slap | |
puts @slap_phrase || 'This is for your own good' | |
end | |
end | |
class Brother < Slapper | |
def before_slap | |
puts 'Think fast!' | |
end | |
end | |
janie = Mom.new | |
janie.slap | |
dave = Brother.new | |
dave.slap |
In this case the ambiguous line was this:
puts @slap_phrase || puts 'This is for your own good,'
It could be interpreted like this:
puts(@slap_phrase || puts('This is for your own good.'))
or this:
puts(@slap_phrase) || puts('This is for your own good.')
Instead of picking one, Ruby refused to run it and told me to be more specific.
In actuality, neither was what I wanted. I accidentally left an extra puts in there. What I wanted was:
puts(@slap_phrase || 'This is for your own good.')
which can be safely expressed without parentheses like so:
puts @slap_phrase || 'This is for your own good.'
note: the syntax error isn't in the gist, it was just in the video. http://www.youtube.com/watch?v=Hx9reRvESOI
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The syntax error was related to the fact that Ruby doesn't require you to use parentheses unless there is an ambiguity. The order of operations (also referred to as "how tightly things are bound") usually makes it clear what to do first: you can call a method which returns a value that can be used as an argument to another method. But sometimes there are two or more valid ways to interpret a sequence, and you therefore must use parentheses.