Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Anonymous classes?

Reply
Thread Tools

Anonymous classes?

 
 
Ben Tilly
Guest
Posts: n/a
 
      10-21-2007
Here is what I'd like to do.

anonymous_class = class.new;

# somehow define methods on anonymous_class

# somehow create an object of type anonymous_class

And I'm running into a roadblock. I can create an anonymous class
easily enough, I just can't figure out how to define methods on it.
Like initialize. Or create objects of that type.

Yes, I know about singleton classes, but they don't do what I want
because I also want to create subclasses of these subclasses, and
instances of these subclasses.

As for why I want this, I'm trying to see whether I can figure out how
to implement a decent prototype based class system in Ruby. A friend
says that you can't. I thought that a clean way to do it would be to
create an anonymous class, and create an object Prototype of that class.
I would make sure that Prototype's new method would create a new class
which is a subclass of Prototype's, and an object of that class with the
prototype being the object that new was called on. And Prototype would
have a def method which would define new methods in the class of the
object that def was called on.

And yes, I know I could do this by dynamically creating normal classes
with calls to eval. But I'd prefer to avoid going there.

Thanks,
Ben
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      10-21-2007

On Oct 20, 2007, at 10:28 PM, Ben Tilly wrote:

> Here is what I'd like to do.
>
> anonymous_class = class.new;
>
> # somehow define methods on anonymous_class
>
> # somehow create an object of type anonymous_class
>
> And I'm running into a roadblock. I can create an anonymous class
> easily enough, I just can't figure out how to define methods on it.
> Like initialize. Or create objects of that type.
>
> Yes, I know about singleton classes, but they don't do what I want
> because I also want to create subclasses of these subclasses, and
> instances of these subclasses.
>
> As for why I want this, I'm trying to see whether I can figure out how
> to implement a decent prototype based class system in Ruby. A friend
> says that you can't. I thought that a clean way to do it would be to
> create an anonymous class, and create an object Prototype of that
> class.
> I would make sure that Prototype's new method would create a new class
> which is a subclass of Prototype's, and an object of that class
> with the
> prototype being the object that new was called on. And Prototype
> would
> have a def method which would define new methods in the class of the
> object that def was called on.
>
> And yes, I know I could do this by dynamically creating normal classes
> with calls to eval. But I'd prefer to avoid going there.
>
> Thanks,
> Ben
> --
> Posted via http://www.ruby-forum.com/.
>


http://codeforpeople.com/lib/ruby/pr...e-2.0.0/README

Cheers-
Ezra Zygmuntowicz
Founder & Ruby Hacker
http://www.velocityreviews.com/forums/(E-Mail Removed)
EngineYard.com



 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      10-21-2007
Ben Tilly wrote:
> Here is what I'd like to do.
>
> anonymous_class = class.new;
> # somehow define methods on anonymous_class


anonymous_class.send(:define_method,:my_method) {|x| puts x}
anonymous_class.class_eval do
def chunky()
"bacon"
end
end

Or just:
anonymous_class = Class.new do
def chunky
"bacon"
end
end


> # somehow create an object of type anonymous_class


my_object = anonymous_class.new


HTH,
Sebastian
--
NP: Mysticum - Let the Kingdom Come
Jabber: (E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      10-21-2007
Ben Tilly wrote:
> Here is what I'd like to do.
>
> anonymous_class = class.new;
>
> # somehow define methods on anonymous_class
>
> # somehow create an object of type anonymous_class
>


How about something like this:

Dog = Class.new {
def self.create_method(name, proc_)
self.send(:define_method, name, proc_)
end
}


p = lambda {puts "Woof, woof."}
Dog.create_method(:bark, p)

d = Dog.new
d.bark


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jimmy Kofler
Guest
Posts: n/a
 
      10-21-2007
> Re: Anonymous classes?
> Posted by Ben Tilly (btilly) on 21.10.2007 07:28
> Here is what I'd like to do.
>
> anonymous_class = class.new;
>
> # somehow define methods on anonymous_class
>
> # somehow create an object of type anonymous_class



Here's yet another take on it: http://snippets.dzone.com/posts/show/3378
and http://ruby-smalltalk.blogspot.com

Cheers

j. k.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ben Tilly
Guest
Posts: n/a
 
      10-21-2007
Jimmy Kofler wrote:
>> Re: Anonymous classes?
>> Posted by Ben Tilly (btilly) on 21.10.2007 07:28

[...]
> Here's yet another take on it: http://snippets.dzone.com/posts/show/3378
> and http://ruby-smalltalk.blogspot.com


Very cute.

Unfortunately doesn't work in old versions of Ruby. (I tried in 1.6.8.)

Ben
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ben Tilly
Guest
Posts: n/a
 
      10-21-2007
Ezra Zygmuntowicz wrote:
> On Oct 20, 2007, at 10:28 PM, Ben Tilly wrote:
>
>> Like initialize. Or create objects of that type.
>> I would make sure that Prototype's new method would create a new class
>> Thanks,
>> Ben
>> --
>> Posted via http://www.ruby-forum.com/.
>>

>
> http://codeforpeople.com/lib/ruby/pr...e-2.0.0/README


If I'd seen that before I implemented mine, I would have just shown
that. However I already have another one. I did borrow some ideas from
that one. Here goes:

class PrototypeClass
attr_reader rototype

def initialize (*prototype)
@prototype = prototype[0]
end

def new (*definitions)
new_class = Class.new(self.class)
for definition in definitions
new_class.module_eval definition
end
return new_class.new(self)
end

def def (definition)
self.class.module_eval "def #{definition} end"
end

def include (mod)
self.class.module_eval "include #{mod}"
end
end

Prototype = PrototypeClass.new(nil)

And here is a sample of usage:

@foo = Prototype.new %q{
attr_accessor :name
}
@bar = @foo.new
@baz = @bar.new

@foo.name = "foo"
@bar.name = "bar"
@baz.name = "baz"

def test_greet
try_greet = lambda {|obj|
if obj.respond_to? :greet
obj.greet(obj.name)
else
puts obj.name
puts "Object #{obj.name} (#{obj.id}) cannot greet"
end
}
try_greet[@foo]
try_greet[@bar]
try_greet[@baz]
end

test_greet()

@bar.def %q{ greet (name)
puts "Greeting from #{name} (id #{self.id})"
}

puts "Defined bar.greet"

test_greet()

module Mod
def greet (name)
puts "Hello from #{name} (#{self.id})"
end
end

@foo.include(Mod)
puts "Included module"

test_greet()

Cheers,
Ben
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ben Tilly
Guest
Posts: n/a
 
      10-21-2007
7stud -- wrote:
> Ben Tilly wrote:
> How about something like this:
>
> Dog = Class.new {
> def self.create_method(name, proc_)
> self.send(:define_method, name, proc_)
> end
> }
>
>
> p = lambda {puts "Woof, woof."}
> Dog.create_method(:bark, p)
>
> d = Dog.new
> d.bark


I tried this, but when I went with my inherited objects, I was not
picking up the IDs of the inherited objects correctly.

Cheers,
Ben
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM
Problem when using and creating nested (anonymous) arrays. moller@notvalid.se Perl 1 02-18-2004 12:35 AM
How do I call sort with an anonymous subroutine stored in a hash ?? Casey Perl 3 01-30-2004 03:39 PM
Pix is anonymous proxy from outside Sean James Cisco 2 12-08-2003 04:22 PM



Advertisments