jbarnette / johnson

Johnson wraps JavaScript in a loving Ruby embrace.
http://github.com/jbarnette/johnson/wikis
465 stars 33 forks source link

Arrays in JS vs arrays in Ruby #8

Closed jbarnette closed 15 years ago

jbarnette commented 15 years ago

I've been using Johnson's gem and have discovered some (sort of) bugs, but still have no time to check what the precise problem is.

There seems to be a compatibility problem between arrays from the Ruby land in JS, as their behaviour is different:

require 'rubygems'
require 'johnson'
Js = Johnson::Runtime.new
Js['alert'] = lambda { |x| puts x }

# Let's test JS arrays
Js.evaluate('array = [1,2,3,4]')
Js.evaluate('for (i in array) { alert(i) }')   # 0 1 2 3
Js.evaluate('for (i in array) { alert(array[i]) }')   # 1 2 3 4

# Let's test Ruby arrays acting as JS arrays
Js['array'] = [1,2,3,4]
Js.evaluate('for (i in array) { alert(i) }')   # 2 3 4 5 (should be 0 1 2 3)
Js.evaluate('for (i in array) { alert(array[i]) }')   # 2 3 4 nil (should be 1 2 3 4)

(from https://johnson.lighthouseapp.com/projects/8759/tickets/50)

matthewd commented 15 years ago

Fixed in 375d56e29bb9b0bcf61393b4608abee380bd328b