The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Mixin Surprise

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Mixin Surprise Posted: Apr 11, 2010 6:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: Mixin Surprise
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
Ah, the things you learn from reading the source and writing your own tests. I've always thought that when you mixin multiple modules with the same method it was a case of "last one wins". It turns out that isn't quite true. To wit:
module ModOne
  def hello
    "hello1"
  end
end

module ModTwo
  def hello
    "hello2"
  end
end

class Foo
  include ModOne, ModTwo
end

class Bar
  include ModOne
  include ModTwo
end

puts Foo.new.hello # => hello1
puts Bar.new.hello # => hello2

So, it seems that if you call include with multiple modules the first one wins, but if you call include multiple times then the last one wins.

Am I the last knucklehead in the Ruby community to figure this out? I checked my pickaxe and that behavior isn't documented so far as I've found. It doesn't strike me as particularly consistent.

It's days like this that make me wonder if I should quit programming and just install solar panels for a living.

Read: Mixin Surprise

Topic: To SproutCore, or not to SproutCore… Previous Topic   Next Topic Topic: PostgreSQL on Linux strangeness

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use