[Ruby] copying methods around
John Whitley
whitley at bangpath.org
Tue Mar 20 17:24:04 PST 2007
Ryan Davis wrote:
> aka factory method
and my brain was off in other lands anyways, as I mistook
manipulating the instance's methods with manipulating the class'
methods...
to Aaron's point, the lightbulb is on now: prototype-ish JS emulation
in Ruby. There's another approach to consider (thinking/typing
quickly, apologies if I gaff it again.. >.< ). Ruby doesn't really
have a first-class concept of method-as-object, which is the rub.
However, you can get something similar with one-off modules used as
containers for the "free" functions.
Consider:
--------- cut here ---------
#!/usr/bin/env ruby -w
require 'ostruct'
module MyMod
def foo
puts "I'm in #{self.class}"
end
end
class << os; include MyMod; end
os.foo
os2 = OpenStruct.new
os2.foo
--------- cut here ---------
If needed as free functions, you can include these into the toplevel
namespace... but you may be better off creating a containing module
to encapsulate all of this crud lest it clutter up the Ruby universe
too much. (I hear that cartoon foxes get cranky over too much
clutter...)
Unfortunately (fortunately? ;-) my knowledge of JS object model
semantics is kinda sketchy, so I'm not sure how good a mapping the
above will provide.
Anyhow, have fun at hack night... back to a wee bit of deadlining
for me... (tease: rails from exerb!)
-- John
More information about the Ruby
mailing list