Building a Better Functor

Hi --

Coming from Javascript (where every instance is trivially extensible

Aw, come from Ruby already! :slight_smile:

directly) that seems somewhat cumbersome.

Whoops, I snipped the part you were talking about... but aren't they
in Ruby too?

   o = Object.new
   def o.x; puts "I'm a new method!"; end

Do many people write things like:

class Object
  def instance_class
    class<<self; self; end
  end
end

in a common library they re-use frequently? Is there an RCR (or has there been discussion) about adding a simple method like that to the language, so you can simply do:

f = Foo.new
f.instance_class.class_eval{ ... }

Yes; I submitted such an RCR, for Kernel#singleton_class. (I know
about the naming issues, but I'd rather not deviate from what Matz
uses at a given time.) See RCR 231: Kernel#singleton_class

Or (what I'm really looking for) perhaps it might be nice to have a define_method method that worked directly for instances. Hrm, but what would the syntax be? Perhaps that's not such a good idea.

I think that Kernel#singleton_class would work for that purpose, and
would also completely transform people's understanding of the whole
class/singleton-class duality.

David

···

On Thu, 10 Feb 2005, Gavin Kistner wrote:

--
David A. Black
dblack@wobblini.net

Heh, right before I got your email, I had just added "instance_class" and "instance_class_eval" to the previous bit of code I pasted. :slight_smile:

···

On Feb 10, 2005, at 8:15 AM, Trans wrote:

Ruby Facets:

  require 'facet/object/special_class'

  f = Foo.new
  f.special_class
  f.special_class_eval { ... }
  f.define_special_method( :meth_name ) { ... }

These are also aliased as "singleton" in place of the word "special".
"Singeton" is the more commonly used term, but it sometimes causes
confusion because of Singleton Pattern. So I choose "special" to help
clarify --being more percise.

See http://calibre.rubyforge.org/

--
(-, /\ \/ / /\/

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:EWKOd.353888$8l.20776@pd7tw1no...

···

Gavin Kistner <gavin@refinery.com> wrote:
>
> in a common library they re-use frequently? Is there an RCR (or has
> there been discussion) about adding a simple method like that to the
> language, so you can simply do:

It's been discussed several times, but no one has come up with a good
name for it yet. (I like proxy_class myself.)

I don't - I prefere singleton_class or instance_class. :slight_smile: It seems it's
about time that matz settles this once and forever by choosing a name and
adding a method to Object in the next release.

Kind regards

    robert

Robert Klemme <bob.news@gmx.net> said:

JavaScript has no classes as far as I know. Ruby has classes and > from that alone it's obvious that it's a bit more complicated.

Indeed JavaScript supports object orientation based on prototypes
rather than classes. This is an idea borrowed by a Smalltalk-like
language called Self and other sources of inspirations.

Don't underestimate JavaScript though or prototype-based inheritance.
Most things that can be done with classes can be done with prototypes.
Some things are straightforward with prototypes: delegation, addition
of a few methods or properties to one or a few instances.

The obvious thing with JavaScript is that it lacks syntactic sugar to
make prototype construction more legible and distinguishable. Most
JavaScript code out there are lame and don't use object programming.
But the real point is that writing object code in JavaScript can be
pretty complicated and likely much less legible than in Ruby.

Regards,
Adriano.

While I admit that I was (shockingly, to me) unaware of that simple
technique, I occasionally do things in JS like:
    myObj[ varWithMethodName ] = function(){ ... }
which requires a define_method type call to translate the string into
the method name.

But thanks for the note!

For more on techniques of OOP in JS, see:
   http://phrogz.net/JS/Classes/OOPinJS.html
and
   http://phrogz.net/JS/Classes/OOPinJS2.html

As Adriano says, it's not 'true' classes, but nested closures and
chained prototypes. Although the techniques are uglier (and inheritance
not truly there), for the majority of cases, you can (without much
work) define what appear to be classes.

"Adriano Ferreira" <a.r.ferreira@gmail.com> schrieb im Newsbeitrag
news:73ddeb6c050210080447cafa7f@mail.gmail.com...

Robert Klemme <bob.news@gmx.net> said:
> JavaScript has no classes as far as I know. Ruby has classes and >

from that alone it's obvious that it's a bit more complicated.

Indeed JavaScript supports object orientation based on prototypes
rather than classes. This is an idea borrowed by a Smalltalk-like
language called Self and other sources of inspirations.

Don't underestimate JavaScript though or prototype-based inheritance.

I didn't intend to. It's just that Ruby has classes while JS has not.

Most things that can be done with classes can be done with prototypes.
Some things are straightforward with prototypes: delegation, addition
of a few methods or properties to one or a few instances.

The obvious thing with JavaScript is that it lacks syntactic sugar to
make prototype construction more legible and distinguishable. Most
JavaScript code out there are lame and don't use object programming.
But the real point is that writing object code in JavaScript can be
pretty complicated and likely much less legible than in Ruby.

Btw:

class JavaScript
  def method_missing(s,*a,&b)
    if /^(.*)=$/ =~ s.to_s
      class <<self;self;end.class_eval do
        define_method($1,&a[0])
      end
    else
      super
    end
  end
end

f=JavaScript.new

=> #<JavaScript:0x1017b1c8>

f.foo= lambda { puts "world" }

=> #<Proc:0x1016d368@(irb):76>

f.bar= lambda { puts "hello" }

=> #<Proc:0x10172a08@(irb):75>

f.bar; f.foo

hello
world
=> nil

:slight_smile:

Kind regards

    robert

The Module class already has an instance-method called
"singleton_methods".

I recall that at one point I was beginning to call a singleton class a
s-class, a singleton-method a s-method, an instance method an i-method, an
instance variable an i-var, especially because that kind of shortcut was
being helpful in designing RubyAST (an internal format for ruby compilers
i designed a few years ago).

In retrospect i like calling them with those short names, because i think
the long names are not that well-chosen, or that i think they are just too
long :-/

The best/worst offender is: if you define a s-method on a class object x,
and you make a class object y that inherits from x, then why the heck
would it be callable from y _AND_ called "singleton", at the same time ?

proxy_class is *not* a good name. it's already used for something else on
a very related topic, in a way that could be confusing (although not that
many people would complain). See chapter 19 of Pickaxe-2000.

···

On Fri, 11 Feb 2005, Robert Klemme wrote:

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:EWKOd.353888$8l.20776@pd7tw1no...
> Gavin Kistner <gavin@refinery.com> wrote:
> > in a common library they re-use frequently? Is there an RCR (or has
> > there been discussion) about adding a simple method like that to the
> > language, so you can simply do:
> It's been discussed several times, but no one has come up with a good
> name for it yet. (I like proxy_class myself.)
I don't - I prefere singleton_class or instance_class. :slight_smile: It seems it's
about time that matz settles this once and forever by choosing a name and
adding a method to Object in the next release.

_____________________________________________________________________
Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju

Phrogz wrote:

For more on techniques of OOP in JS, see:
   OOP in JS, Part 1 : Public/Private Variables and Methods
and
   OOP in JS, Part 2 : Inheritance

And for an example of how powerful JavaScript really is you might want to have a look at ruby.js:

http://whytheluckystiff.net/clog/ruby/rubyDotJs.html

And thanks for the wonderful JavaScript information available on your site -- it helps a lot!