Created
April 7, 2010 03:12
-
-
Save lsegal/358467 to your computer and use it in GitHub Desktop.
Scoped implicit conversion in Ruby, code is http://github.com/lsegal/ruby/tree/implicit
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
class MyString < String | |
def foo; reverse + "X" end | |
end | |
module Foo | |
implicit String => MyString | |
class A | |
attr_reader :myattr | |
def initialize | |
@myattr = "hello" | |
end | |
end | |
end | |
p Foo::A.new.myattr.foo | |
p "hello".foo | |
# "ollehX" | |
# test.rb:17:in `<main>': undefined method `foo' for "hello":String (NoMethodError) |
That's frickin' sweeet. Is this enough to fix the monkey patch contamination problem?
That took me a minute to parse. Pretty cool.
Great! Threads like this(http://news.ycombinator.com/item?id=1548670) show how much value this would add on Ruby's trunk. Hope it makes it there soon (on both 1.8.8 and 1.9.3)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can find the code on my Ruby gh fork: http://github.com/lsegal/ruby/tree/implicit