-
-
Save foca/558149 to your computer and use it in GitHub Desktop.
| 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" |
FWIW, from what I gleaned from the slides I'd expect:
class Something
mix Numbers, :foo => :numeric_foo
def foo
"foo"
end
end
# So now…
Something.new.bar #=> 4
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:
class Something
mix Numbers
def foo
"foo"
end
end
# So now…
Something.new.bar #=> "foofoo"
Because you mix the Numbers module in before the foo method exists, there is no conflict. But, we really don't know yet. :)
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.
Don't you mean: