#50 ✓resolved
Jose

Arrays in JS vs arrays in Ruby

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

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.

Comments and changes to this ticket

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Pages