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®
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?
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:
o = obj()
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:
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?
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.
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:
(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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.