Skip to content

Instantly share code, notes, and snippets.

@foca
Created August 30, 2010 22:26
Show Gist options
  • Save foca/558149 to your computer and use it in GitHub Desktop.
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"
@blowmage
Copy link

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