Hash from two arrays: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Ruby}}: removed custom method since it is built in nowadays) |
||
Line 1,447: | Line 1,447: | ||
puts hash[ [1,2,3] ] # => 123 |
puts hash[ [1,2,3] ] # => 123 |
||
</lang> |
</lang> |
||
Or define a new method in class Array: |
|||
<lang ruby> |
|||
class Array |
|||
def zip_hash(other) |
|||
Hash[self.zip(other)] |
|||
end |
|||
end |
|||
hash = %w{ a b c }.zip_hash( %w{ 1 2 3 } ) |
|||
p hash # => {"a"=>"1", "b"=>"2", "c"=>"3"} |
|||
</lang> |
|||
Hash[] is calling "to_hash" inside. |
|||
Therefore, it doesn't work out when making a method name "to_hash". |
|||
In Ruby 2.1 the method "to_h" was introduced: |
In Ruby 2.1 the method "to_h" was introduced: |