# OpenClass

**URL:** <https://rubytalk.org/t/openclass/21054>\
**Category:** ruby-talk\
**Created:** [24 September 2005 06:22 UTC](https://rubytalk.org/t/openclass/21054 "2005-09-24T06:22:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raymond\_medeiros](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raymond\_medeiros](https://rubytalk.org/u/raymond_medeiros)\
**Post date:** [24 September 2005 06:22 UTC](https://rubytalk.org/t/openclass/21054/1 "2005-09-24T06:22:59Z")

</div>

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

irb(main):001:0\> o = OpenClass.new  
=\> #\<OpenClass:0xb7d305cc\>  
irb(main):002:0\> o.test\_method = %{ puts "test" }  
=\> " puts \"test\" "  
irb(main):003:0\> o.test\_method  
test  
=\> nil  
irb(main):004:0\>

> **···**
>
> --  
> 2.times { p true }

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [24 September 2005 07:01 UTC](https://rubytalk.org/t/openclass/21054/2 "2005-09-24T07:01:41Z")

</div>

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)  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set( "@#{name}", new\_proc }  
&nbsp;&nbsp;&nbsp;&nbsp;self.instance\_eval %{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{name}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{new\_proc}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def new  
&nbsp;&nbsp;&nbsp;&nbsp;o=OpenClass.new  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_variables.each { |iv|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.add\_member( iv.gsub(/^@/,''), instance\_variable\_get(iv) )  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;yield(o) if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;o  
&nbsp;&nbsp;end

&nbsp;&nbsp;def open  
&nbsp;&nbsp;&nbsp;&nbsp;yield(self) if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;end

&nbsp;&nbsp;...

&nbsp;&nbsp;def obj  
&nbsp;&nbsp;&nbsp;&nbsp;o=OpenClass.new  
&nbsp;&nbsp;&nbsp;&nbsp;yield(o)  
&nbsp;&nbsp;&nbsp;&nbsp;o  
&nbsp;&nbsp;end

&nbsp;&nbsp;myobj = obj { |o|  
&nbsp;&nbsp;&nbsp;&nbsp;o.test\_method = %{ puts "test" }  
&nbsp;&nbsp;}

&nbsp;&nbsp;myobj2 = myobj.new { |o|  
&nbsp;&nbsp;&nbsp;&nbsp;o.test\_method2 = %{ puts "test2" }  
&nbsp;&nbsp;}

&nbsp;&nbsp;myobj2.open { |o|  
&nbsp;&nbsp;&nbsp;&nbsp;o.test\_method3 = %{ puts "test3" }  
&nbsp;&nbsp;}

Unfortuately Ruby has no way to close off block scopes, so all these  
blocks are wide open to main.

T.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [24 September 2005 07:41 UTC](https://rubytalk.org/t/openclass/21054/3 "2005-09-24T07:41:40Z")

</div>

I don't see the advantange of this syntax over

def o.test\_method() puts "test\_method" end

Can you explain?

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> 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
> > 
> > o = OpenClass.new  
> > o.test\_method = %{ puts "test\_method" }

---

<div class="post-metadata">

**Author:** ![raymond\_medeiros](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raymond\_medeiros](https://rubytalk.org/u/raymond_medeiros)\
**Post date:** [24 September 2005 08:18 UTC](https://rubytalk.org/t/openclass/21054/4 "2005-09-24T08:18:44Z")

</div>

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) \<[http://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" }
> > 
> > I don't see the advantange of this syntax over
> > 
> > def o.test\_method() puts "test\_method" end
> > 
> > Can you explain?
> > 
> > Kind regards
> > 
> > robert
> 
> --  
> 2.times { p true }

---

<div class="post-metadata">

**Author:** ![Doug\_Kearns1](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Doug\_Kearns1](https://rubytalk.org/u/Doug_Kearns1)\
**Post date:** [24 September 2005 09:36 UTC](https://rubytalk.org/t/openclass/21054/5 "2005-09-24T09:36:33Z")

</div>

\<snip\>

> I don't see the advantange of this syntax over
> 
> def o.test\_method() puts "test\_method" end
> 
> Can you explain?

Well, I guess it lets you do something like:

name = "Robert"  
o = OpenClass.new  
o.test\_method = %{ puts "Hello #{name}" }  
o.test\_method

Regards,  
Doug

> **···**
>
> On Sat, Sep 24, 2005 at 04:41:40PM +0900, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [24 September 2005 14:21 UTC](https://rubytalk.org/t/openclass/21054/6 "2005-09-24T14:21:40Z")

</div>

> \<snip\>
> 
> > I don't see the advantange of this syntax over
> > 
> > def o.test\_method() puts "test\_method" end
> > 
> > Can you explain?
> 
> Well, I guess it lets you do something like:
> 
> 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

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Doug Kearns \<dougkearns@gmail.com\> wrote:
> 
> > On Sat, Sep 24, 2005 at 04:41:40PM +0900, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [25 September 2005 02:21 UTC](https://rubytalk.org/t/openclass/21054/7 "2005-09-25T02:21:41Z")

</div>

> 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.

T.

---

<div class="post-metadata">

**Author:** ![raymond\_medeiros](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@raymond\_medeiros](https://rubytalk.org/u/raymond_medeiros)\
**Post date:** [25 September 2005 03:22 UTC](https://rubytalk.org/t/openclass/21054/8 "2005-09-25T03:22:49Z")

</div>

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.
> > 
> > T.
> 
> --  
> 2.times { p true }
