cowboyd/redjs — pull request #2 by kares (2011-12-14 13:21:54+00:00)
Title: not all Ruby methods of arity 0 should be seen as properties in JavaScript

hey guys, these are the changes accumulated from refactoring cowboyd/therubyrhino#10

as the title suggests the most important (and backward incompatible change) would be changing how wrapped Ruby methods of arity 0 are called from JS.
I do realize it's impossible to do make this "perfect" as one can't for sure distinguish a regular no-arg method from an attr_reader method. but I think we can for sure do better than treating all of those as properties.

my first attempt while hacking **therubyrhino** was to get/set Ruby instance variables directly while doing JavaScript's property get/put, then I changed this to using attribute accessors. this can be done almost perfectly except for a single case, when there's no writer and the variable is not yet initialized:

class Ruby
def property
@property
end
end

ruby = Ruby.new # JS ruby['property'] would return a function

although even this case is "fixable" by users adding :

class Ruby
def initialize
@property = nil
end
end

ruby = Ruby.new # now JS ruby['property'] would return null

I'm pretty sure this would require refactoring in **therubyracer** and might "suprise" existing users, but it should be pretty clear if they understand JavaScript e.g.

class Foo
def bar
'bar'
end
end

they would need to change all their `foo.bar` JS to `foo.bar()` ...

maybe even a "backward" compatible flag might be implemented for a while in **therubyracer** to deal with migrating code to this new behavior.
I do not see a reason for doing this in **therubyrhino** since it's user base seems to be smaller and the last released version isn't really working that smoothly (failing 19 JS specs - some related to property resolution).

of course, feel free to try convincing me that the current state is better if you feel so ...