# How to pass a hash into a function

**URL:** <https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151>\
**Category:** Ruby\
**Created:** [February 14, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151 "2008-02-14T22:32:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 14, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151/1 "2008-02-14T22:32:40Z")

</div>

Hi, I have a hash and i need to pass it to a function in another  
controller. Here’s what I have to do that

\<%= link\_to ‘View’, :controller =\> :attempts, :action =\> :show\_cbam, :id  
=\> a.code, :percentiles =\>@percentile\_hash%\>

where @percentile\_hash is the hash I have. Then I call it in the other  
controller like this

def show\_cbam

@percentiles = params[:percentiles]

This seems to work ok, i can see the hash in my view but it doesnt work  
properly.  
Here’s an example of my hash

@percentiles = {1=\>[88,99,100]}

When i call @percentiles it outputs 18899100 so it seems fine but when i  
call @percentiles[1] it returns 54. I have no idea why, it should return  
the array [88,99,100]. Furthermore, for values like @percentiles[2]  
it’ll return like 55 even though there is no value associated with 2.  
does anyone understand why this is happening? thank you 🙂

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 14, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151/2 "2008-02-14T23:02:06Z")

</div>

h = {1=\>[88, 99, 100]}

def show(a\_hash)  
puts a\_hash[1]  
end

show(h)

–output:–  
88  
99  
100

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 14, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151/3 "2008-02-14T23:18:47Z")

</div>

7stud – wrote:

> h = {1=\>[88, 99, 100]}
> 
> def show(a\_hash)  
> puts a\_hash[1]  
> end
> 
> show(h)
> 
> –output:–  
> 88  
> 99  
> 100

yeah, im not doing standard ruby like that, I’m doing it through html so  
I think it’s getting screwed up in that process. I’m just going to  
calculate the hash in the other controller instead and try that.

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 15, 2008, 6:10am UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151/4 "2008-02-15T06:10:01Z")

</div>

Mark Mr wrote:

> yeah, im not doing standard ruby like that

Then why would you post your question in a standard ruby forum?

---

<div class="post-metadata">

**Author:** ![pimeamark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pimeamark](https://www.ruby-forum.com/u/pimeamark)\
**Post date:** [February 15, 2008, 3:57am UTC](https://www.ruby-forum.com/t/how-to-pass-a-hash-into-a-function/129151/5 "2008-02-15T03:57:40Z")

</div>

On Thu, Feb 14, 2008 at 1:32 PM, Mark Mr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the array [88,99,100]. Furthermore, for values like @percentiles[2]  
> it’ll return like 55 even though there is no value associated with 2.  
> does anyone understand why this is happening? thank you 🙂

Are you sure those are the EXACT numbers you are getting? Because it  
sounds like you might be getting the string “18899100” rather than the  
hash, which would give numbers close to that but not exactly. E.g.:

irb(main):004:0\> “18899100”[1]  
=\> 56  
irb(main):005:0\> “18899100”[2]  
=\> 56  
irb(main):006:0\> “18899100”[3]  
=\> 57  
irb(main):007:0\> “18899100”[0]  
=\> 49
