Can't convert hash into integer

Hi,

I have the following code in model:

input[:hello].each do |abc|
blablablab
end

and it terminated with error “can’t convert hash into integer”

anyone has got a clue? Thanks.

Hi –

On Tue, 26 Sep 2006, Ianne L. wrote:

anyone has got a clue? Thanks.

My first suspicion is that, somewhere, you’re trying to use a hash
where you need an integer :slight_smile: But “blablablab”, and no information
about what line the error pointed to, makes it hard to say much more.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Hi –

On Tue, 26 Sep 2006, Ianne L. wrote:

options[opt_k] = OtherClass.new(input[:option][opt_k])
You’re assigning to options[opt_k] twice, which means the first
assignment will get clobbered.

end unless input[:option].nil?

#I suspect that the problem is assoc with input[:option][opt_k], which
returns error when I want to put it on screen

That sounds right. input[:option] is an array, and you’re trying to
index it with a hash (opt_k) instead of an integer. (I’m assuming
options is a hash; if it’s an array, then you’ve got the same problem
there too.)

I’m not clear on what you’re trying to do in that each loop, so I’m
not sure what the right code would be, but you’ll need to make sure
not to use a hash to index an array.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

sorry for the incomplete info. Here’re the details:

#I’ve construct the data object in the controller:
for i in 0…10
@option[i] = {
:name => @params[(“name”+i.to_s).to_sym],
:value => @params[(“value”+i.to_s).to_sym]
}
end

#then pass the object to model function
Somemod.funct(
…some other parameters…
:option => @option)

#this is the model function
self.funct(input)

input[:option].each do |opt_k|
options[opt_k] = Hash.new(0)
options[opt_k] = OtherClass.new(input[:option][opt_k])

end unless input[:option].nil?

#I suspect that the problem is assoc with input[:option][opt_k], which
returns error when I want to put it on screen

Thanks.

unknown wrote:

Hi –

On Tue, 26 Sep 2006, Ianne L. wrote:

anyone has got a clue? Thanks.

My first suspicion is that, somewhere, you’re trying to use a hash
where you need an integer :slight_smile: But “blablablab”, and no information
about what line the error pointed to, makes it hard to say much more.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org