Created
August 30, 2010 22:26
-
-
Save foca/558149 to your computer and use it in GitHub Desktop.
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
module Numbers | |
def foo | |
2 | |
end | |
def bar | |
foo * 2 | |
end | |
end | |
class Something | |
mix Numbers, :foo => :numeric_foo | |
def foo | |
"foo" | |
end | |
end | |
# So now… | |
Something.new.bar #=> 4 | |
# Or | |
Something.new.bar #=> "foofoo" |
Ah, yes, I meant Something.new.bar
:)
If you don't override the method, it will blow up, so your second example should be mix Numbers, :foo => nil
, but yeah, it's a good point. My personal expectation, is that it should return 4
, not "foofoo"
, because it would only make sense. But that's just me :)
The code executes from top to bottom. The mix
happens before foo
is defined. The mix
call will error if it is mixing in a method that already exists, but in your example the foo
method doesn't exist yet. I think you would only need to set foo
to nil
if it was like this:
class Something
def foo
"foo"
end
mix Numbers, foo:nil
end
# So now…
Something.new.bar #=> "foofoo"
Just my understanding so far. I could very well be wrong.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW, from what I gleaned from the slides I'd expect:
Because in this case bar is calling numeric_foo. If you wanted to override foo that bar was calling then you'd do something like this:
Because you mix the Numbers module in before the foo method exists, there is no conflict. But, we really don't know yet. :)