1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

Add V8::Object#keys and V8::Object#values

This commit is contained in:
Charles Lowell 2012-07-05 23:49:50 -05:00
parent cd95d083bb
commit 35a37eb20d
2 changed files with 29 additions and 0 deletions

View file

@ -22,6 +22,20 @@ class V8::Object
return value
end
def keys
@context.enter do
names = @native.GetPropertyNames()
0.upto( names.Length() - 1).to_enum.map {|i| @context.to_ruby names.Get(i)}
end
end
def values
@context.enter do
names = @native.GetPropertyNames()
0.upto( names.Length() - 1).to_enum.map {|i| @context.to_ruby @native.Get(names.Get(i))}
end
end
def each
@context.enter do
names = @native.GetPropertyNames()

15
spec/v8/object_spec.rb Normal file
View file

@ -0,0 +1,15 @@
require 'spec_helper'
describe V8::Object do
before do
@object = V8::Context.new.eval('({foo: "bar", baz: "bang", qux: "qux1"})')
end
it "can list all keys" do
@object.keys.sort.should eql %w(baz foo qux)
end
it "can list all values" do
@object.values.sort.should eql %w(bang bar qux1)
end
end