Skip to content

Commit

Permalink
Introduce Opal.to_ary runtime helper
Browse files Browse the repository at this point in the history
to_ary is used to implicitly convert objects into arrays where they are
expected, e.g. mass-assignments
  • Loading branch information
adambeynon committed Sep 11, 2013
1 parent 3340e71 commit ba7ea76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 9 additions & 0 deletions corelib/runtime.js
Expand Up @@ -330,6 +330,15 @@
return block.apply(null, args);
};

// Helper to convert the given object to an array
Opal.to_ary = function(value) {
if (value._isArray) {
return value;
}

return [value];
};

/*
Call a ruby method on a ruby object with some arguments:
Expand Down
3 changes: 1 addition & 2 deletions lib/opal/parser.rb
Expand Up @@ -1479,8 +1479,7 @@ def process_masgn(sexp, level)
len = rhs.length - 1 # we are guaranteed an array of this length
code << f("#{tmp} = ", sexp) << process(rhs)
elsif rhs[0] == :to_ary
code << f("((#{tmp} = ", sexp) << process(rhs[1])
code << f(")._isArray ? #{tmp} : (#{tmp} = [#{tmp}]))", sexp)
code << [f("#{tmp} = $opal.to_ary("), process(rhs[1]), f(")")]
elsif rhs[0] == :splat
code << f("(#{tmp} = ", sexp) << process(rhs[1])
code << f(")['$to_a'] ? (#{tmp} = #{tmp}['$to_a']()) : (#{tmp})._isArray ? #{tmp} : (#{tmp} = [#{tmp}])", sexp)
Expand Down

0 comments on commit ba7ea76

Please sign in to comment.