it seemed like a good idea at the time but honestly, hindsight being 20/20 i
have no idea what i'd do with this:
class OpenClass #create the new method
def add_member(name, new_proc)
self.instance_eval %{
def #{name}
#{new_proc}
end
}
end
#there are no predefined methods so we'll add them dynamically
def method_missing(method, *args)
new_proc = args[0]
mname = method.to_s.gsub(/=/, "")
add_member(mname, new_proc)
end
end
o = OpenClass.new
o.test_method = %{ puts "test_method" }
o.test_method
An interesting variation on OpenStruct. In my work with such things
I've found that defined methods to return the values is slowere then
just using a lookup table. But in this case I think it would not be
since the dyanmic value needs to be evaluated.
What can one do with it? Well, for starters it provides an interesting
alternative as the bases of prototype-based Ruby programming
--something I've been playng with.
...
def add_member(name, new_proc)
instance_variable_set( "@#{name}", new_proc }
self.instance_eval %{
def #{name}
#{new_proc}
end
}
end
def new
o=OpenClass.new
instance_variables.each { |iv|
o.add_member( iv.gsub(/^@/,''), instance_variable_get(iv) )
}
yield(o) if block_given?
o
end
it seemed like a good idea at the time but honestly, hindsight being
20/20 i have no idea what i'd do with this:
class OpenClass #create the new method
def add_member(name, new_proc)
self.instance_eval %{
def #{name}
#{new_proc}
end
}
end
#there are no predefined methods so we'll add them dynamically
def method_missing(method, *args)
new_proc = args[0]
mname = method.to_s.gsub(/=/, "")
add_member(mname, new_proc)
end
o = OpenClass.new
o.test_method = %{ puts "test_method" }
me either, that was sort of my point hindsight being 20/20, seemed like the
thing to do at the time. althought i'd be interested in hearing more about
what transfire is talking about perhaps there is more to it than i'm seeing.
ruby is so insanely dynamic that it seems reasonable to do insane things for
no apparent reason. www.whytheluckystiff.net <http://www.whytheluckystiff.net> is an excellent
example of totaly complete insanity and
randomness. viva la ruby!
···
On 9/24/05, Robert Klemme <bob.news@gmx.net> wrote:
raymond medeiros <zenlinux@gmail.com> wrote:
> it seemed like a good idea at the time but honestly, hindsight being
> 20/20 i have no idea what i'd do with this:
>
> class OpenClass
> #create the new method
> def add_member(name, new_proc)
> self.instance_eval %{
> def #{name}
> #{new_proc}
> end
> }
> end
>
> #there are no predefined methods so we'll add them dynamically
> def method_missing(method, *args)
> new_proc = args[0]
> mname = method.to_s.gsub(/=/, "")
> add_member(mname, new_proc)
> end
> end
>
> o = OpenClass.new
> o.test_method = %{ puts "test_method" }
name = "Robert"
o = OpenClass.new
o.test_method = %{ puts "Hello #{name}" }
o.test_method
class Foo;end
=> nil
f = Foo.new
=> #<Foo:0x1018f060>
name = "robert"
=> "robert"
class <<f;self;end.instance_eval { define_method(:test_method) { puts name } }
=> #<Proc:0x10181440@(irb):47>
f.test_method
robert
=> nil
Not shorter or nicer but syntax checked on compile time. It's usually better to try to avoid eval (some $SAFE levels even forbid eval if I'm not mistaken). You can easily put the code above into a helper method in class Module so this becomes more convenient.
Kind regards
robert
···
Doug Kearns <dougkearns@gmail.com> wrote:
On Sat, Sep 24, 2005 at 04:41:40PM +0900, Robert Klemme wrote:
me either, that was sort of my point hindsight being 20/20, seemed like the
thing to do at the time. althought i'd be interested in hearing more about
what transfire is talking about perhaps there is more to it than i'm seeing
Well, it just a differnt way to think about coding --prototypes vs
calsses. Your OpenClass provides a means very similar to what I was
doing in exploring this. That's all really. Coding in Ruby generally
though, I think OpenStruct would prove more useful.
i have a friend who discovered a great use for it, let you know how it goes.
···
On 9/24/05, Trans <transfire@gmail.com> wrote:
> me either, that was sort of my point hindsight being 20/20, seemed like
the
> thing to do at the time. althought i'd be interested in hearing more
about
> what transfire is talking about perhaps there is more to it than i'm
seeing
Well, it just a differnt way to think about coding --prototypes vs
calsses. Your OpenClass provides a means very similar to what I was
doing in exploring this. That's all really. Coding in Ruby generally
though, I think OpenStruct would prove more useful.