Ruby.extend Javascript

T

Trans

Doing some Javascript coding today and started to wonder about it's
open objects. If it works so well for Javascript, then why not Ruby
too? So I threw this together:

# Add open_accessor.

class Module
private
def open_accessor(r)
class_eval %{
def #{r}=(v)
o = (class << self; self; end)
if Func === v
o.send:)define_method,:#{r}, &v)
else
o.send:)attr_reader, :#{r})
@#{r} = v
end
end
}
end
end

# All objects are open.

class Object
def method_missing( s, *a, &b )
w = s.to_s
if w[-1,1] == '='
r = w.chomp('=')
v = a[0]
o = (class << self; self; end)
o.send:)open_accessor, r)
send(w,v)
else
super
end
end
end

# Func is a Proc, but we need a distinction to determine
# when one represents a new method vs passing a proc as object.

class Func < Proc
def to_proc
Proc.new(self)
end
end

# Create a new object of +classification+.

def obj(classification=Object)
classification.new
end

# Define a function.

def fn(&b)
Func.new(&b)
end

# Show off.

if $0 == __FILE__

o = obj()

o.a = 1
p o.a

o.b = fn{ puts "hello" }
o.b

end

So would Ruby benefit from being able to do this inherently? Or are
there reasons against it and it is actaully Javascript that's suffering
for it? Or is there some difference between Ruby and Javascript that
makes it good for one, but not the other?

T.
 
M

Marcin Mielżyński

Trans said:
Doing some Javascript coding today and started to wonder about it's
open objects. If it works so well for Javascript, then why not Ruby
too? So I threw this together:


# Define a function.

def fn(&b)
Func.new(&b)
end

# Show off.

if $0 == __FILE__

o = obj()

o.a = 1
p o.a

o.b = fn{ puts "hello" }
o.b

end

So would Ruby benefit from being able to do this inherently? Or are
there reasons against it and it is actaully Javascript that's suffering
for it? Or is there some difference between Ruby and Javascript that
makes it good for one, but not the other?

T.

Javascript is a prototype based language where prototype is a template
for objects, Ruby on the other side is a class based language. It's
nothing wrong with Javascript,IO,Self and others:
http://www.dekorte.com/docs/protos/ or Ruby - they just represent
different paradigms. But there are analogies as well:

function Obj(){
}

Obj.prototype = {
attr : 7,
method : function(){
print("hello!")
}
}

The Obj prototype holds an associative array for new objects, so:

obj = new Obj()
obj.method()

and this is pretty much an equivalent of a class based approach Ruby offers

but when:

obj.method = function(){
print("hello2")
}

obj.method()

will print hello2. The _method_ attribute in object _obj_ now hides the
original one (the one in prototype object)

btw. This works pretty much the same in Python (there are dictionaries
called __dict__ that hold attributes for classes and instances
respectively).


Ruby provides the same functionality using singleton objects


class Obj
def method
p "hello"
end
end


obj = Obj.new

obj.method

and then:

class << obj

def method
p "hello2"
end

end

obj.method

the _method_ method is now being hold in the singleton class (the one
you access by (class << self; self; end) )


there is also an implementation of a prototype-based Ruby sublunguage
called sloop:
http://chneukirchen.org/blog/archiv...e-for-object-orientation-with-prototypes.html


lopex
 
D

David Vallner

--------------enig1FEAE35E68CA58FECA1E2F42
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
So would Ruby benefit from being able to do this inherently? Or are
there reasons against it and it is actaully Javascript that's suffering=
for it? Or is there some difference between Ruby and Javascript that
makes it good for one, but not the other?
=20

It's the expected way of doing things in prototype-based OO - a class
definition is sum of method additions to its parent prototype.

Ruby is mainly a class-based OO language, which means readers of the
code expect to see class definitions, not prototype manipulations.

It is generally possible to implement a class-based object system in a
prototype-based language and the other way around, but generally other
users of a language will easier understand code written in a language's
primary flavour. Also, that flavour will probably be more efficient
because it gets first-class support (and is usually the flavour
implemented in C these days.)

There's nothing inherently "wrong" about using prototype-style OO in
Ruby while heavily abusing singleton methods or automagical delegates,
but it will confuse people reading that code. There's also no RDoc
support for sniffing that code structure, and various other quirks. But
it's a valid approach in my book.

David Vallner


--------------enig1FEAE35E68CA58FECA1E2F42
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFTnsHy6MhrS8astoRAtiTAJ4h3d5cFtFyDNoCnGMmky8NdKHAdACdHhms
vZjeNXmjX2rJ79F2GPURijM=
=FuAu
-----END PGP SIGNATURE-----

--------------enig1FEAE35E68CA58FECA1E2F42--
 
T

Trans

Marcin, David, greg thanks for the feedback.

I'm more interested in the question as to why having that capability
directly built-in would or would-not be of benefit to Ruby.

I'm aware of the class vs. prototype differences and ara's
implementation of prototype.rb --i've even written one myself years ago
(sloop was new to me though thanks). And granted, we could use such a
lib even if it were a tad suprising to on looking Ruby coders. But I'm
not really looking at a whole prototype approach so much as just
thinking that this one particular capability would be of use. And the
reason I say that, is that I find myself more and more often using
hashes for parameters rather than straight arguments. In turn I usually
prefer accessing them with dot notation rather than hash, so I end up
doing:

def foo( keys )
keys = keys.to_openobject
keys.bar
keys.baz
...

(OpenObject is an improved OpenStruct BTW) There's a lot of overhead
going on here. So when I was working with Javascript today, I couldn't
help but think, why do they have it so good in this respect, but Ruby
doesn't?

T.
 

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

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top