Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Arrays in JS vs arrays in Ruby

#50

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)

Also, if I add a function to Array.prototype, it is not available in arrays from the Ruby land, so there are incompatibilities between JS & Ruby lands. Similarly, hashes show different behaviours in each land.

Reported by Jose · May 27th, 2009 @ 01:15 AM

State: resolved
Milestone: none
Assigned to: John Barnette John Barnette

Activity

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.