Skip to content

Instantly share code, notes, and snippets.

@lsegal
Created April 7, 2010 03:12
Show Gist options
  • Save lsegal/358467 to your computer and use it in GitHub Desktop.
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
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)
@lsegal
Copy link
Author

lsegal commented Apr 7, 2010

You can find the code on my Ruby gh fork: http://github.com/lsegal/ruby/tree/implicit

@fierydrake
Copy link

That's frickin' sweeet. Is this enough to fix the monkey patch contamination problem?

@phillmv
Copy link

phillmv commented Apr 7, 2010

That took me a minute to parse. Pretty cool.

@danielribeiro
Copy link

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