Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby inject with initial being a hash

Tags:

ruby

Can any one tell me why the following:

['a', 'b'].inject({}) {|m,e| m[e] = e } 

throws the error:

IndexError: string not matched         from (irb):11:in `[]='         from (irb):11:in `block in irb_binding'         from (irb):11:in `each'         from (irb):11:in `inject'         from (irb):11         from C:/Ruby192/bin/irb:12:in `<main>' 

whereas the following works?

a = {} a["str"] = "str" 
like image 685
Candide Avatar asked Feb 24 '12 16:02

Candide


People also ask

How do you instantiate a hash in Ruby?

Creating a Hash In Ruby you can create a Hash by assigning a key to a value with => , separate these key/value pairs with commas, and enclose the whole thing with curly braces.

What does inject in Ruby do?

At the end of the process, inject returns the accumulator, which in this case is the sum of all the values in the array, or 10. In this case, we are defaulting our accumulator to an empty hash, then populating it each time the block executes.

How do you check if something is a hash in Ruby?

Overview. A particular value can be checked to see if it exists in a certain hash by using the has_value?() method. This method returns true if such a value exists, otherwise false .

What can be a hash key in Ruby?

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. Hashes enumerate their values in the order that the corresponding keys were inserted.

How does the inject method work in Ruby?

This article will demonstrate the different uses of the inject method in Ruby. The inject method takes a block of two arguments. The first argument is an accumulator, the running total of the expression being evaluated, and the second is the current array item. In the example above, the initial value is 0.

How to initialize a hash in Java?

The second form of initializing a Hash is by passing in an object. This object will then be used as the default value. From the docs: If obj is specified, this single object will be used for all default values. I most commonly reach for this style when using a hash as a counter.

How does the inject method work in C++?

The inject method takes a block of two arguments. The first argument is an accumulator, the running total of the expression being evaluated, and the second is the current array item. In the example above, the initial value is 0.

How hard is it to learn Ruby?

It’s not too hard- Ruby is very similar to Java and C. If you’re familiar with programming in Java or C, you should be able to learn Ruby in no time. The INJECT keyword in Ruby calls the INJECT method that is most commonly used with arrays, with the enumerable module and with converting array elements into hashes.


1 Answers

Your block needs to return the accumulating hash:

['a', 'b'].inject({}) {|m,e| m[e] = e; m } 

Instead, it's returning the string 'a' after the first pass, which becomes m in the next pass and you end up calling the string's []= method.

like image 72
Rob Davis Avatar answered Sep 24 '22 14:09

Rob Davis