OpenClass

R

raymond medeiros

------=_Part_843_29935623.1127542973065
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D args[0]
mname =3D method.to_s.gsub(/=3D/, "")
add_member(mname, new_proc)
end
end

o =3D OpenClass.new
o.test_method =3D %{ puts "test_method" }
o.test_method

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

Trans

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

def open
yield(self) if block_given?
self
end

...

def obj
o=OpenClass.new
yield(o)
o
end

myobj = obj { |o|
o.test_method = %{ puts "test" }
}

myobj2 = myobj.new { |o|
o.test_method2 = %{ puts "test2" }
}

myobj2.open { |o|
o.test_method3 = %{ puts "test3" }
}

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

T.
 
R

Robert Klemme

raymond medeiros said:
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
 
R

raymond medeiros

------=_Part_1008_26623681.1127549921982
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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=
 
D

Doug Kearns

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

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
 
R

Robert Klemme

Doug Kearns said:
On Sat, Sep 24, 2005 at 04:41:40PM +0900, Robert Klemme wrote:



Well, I guess it lets you do something like:

name = "Robert"
o = OpenClass.new
o.test_method = %{ puts "Hello #{name}" }
o.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
 
T

Trans

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

raymond medeiros

------=_Part_3086_23148454.1127618567003
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

i have a friend who discovered a great use for it, let you know how it goes=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top