# Ruby Hash wierdness

**URL:** <https://www.ruby-forum.com/t/ruby-hash-wierdness/50332>\
**Category:** Ruby\
**Created:** [December 15, 2005, 11:14am UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332 "2005-12-15T11:14:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [December 15, 2005, 11:14am UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332/1 "2005-12-15T11:14:18Z")

</div>

Hi

I’m having problems with using a hash. I put in a class and when I  
retrieve it, it’s automagically become a string

ie

\<%= button\_link \_(“Export CSV”),  
SearchParams.get\_url\_hash({ :action =\> :export\_csv, :klass =\>  
Customer }, params, @search\_fields) %\>

Customer is a class that is available to my view

def export\_csv  
klass = params[:klass]  
cond = SearchParams.get\_search\_conditions(klass, params,  
@search\_fields)  
set\_csv\_headers(klass)  
csv\_export(klass, cond)  
end

but here klass (and params[:klass]) is a string? What I really want to  
do is

\<%= button\_link \_(“Export CSV”),  
SearchParams.get\_url\_hash({ :action =\> :export\_csv(Customer) },  
params, @search\_fields) %\>

Any ideas why my lovely Customer class is being butchered into a string  
☹

Kev

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [December 15, 2005, 11:44am UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332/2 "2005-12-15T11:44:34Z")

</div>

Kev J. wrote:

> Customer is a class that is available to my view
> 
> def export\_csv  
> klass = params[:klass]

Try

klass = Object.const\_get(params[:klass])

> ```
> SearchParams.get_url_hash({ :action => :export_csv(Customer)
> 
> ```
> 
> }, params, @search\_fields) %\>
> 
> Any ideas why my lovely Customer class is being butchered into a  
> string ☹

Probably because there is an HTTP request in between?

```
robert
```

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [December 15, 2005, 11:47am UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332/3 "2005-12-15T11:47:36Z")

</div>

> > def export\_csv  
> > klass = params[:klass]
> 
> Try
> 
> klass = Object.const\_get(params[:klass])

beautiful, thank you so much

Kev

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [December 15, 2005, 12:38pm UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332/4 "2005-12-15T12:38:51Z")

</div>

Yep, urls only contains strings. Often horribly ugly ones with %20 etc  
all through it, but they are strings.

I like the Object.const\_get method - cleaner and more specific than an  
eval etc.

Nic

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [December 15, 2005, 1:54pm UTC](https://www.ruby-forum.com/t/ruby-hash-wierdness/50332/5 "2005-12-15T13:54:03Z")

</div>

My guess is (and I am new to rails myself) is that the browser is not  
smart enough to send an object, it only knows how to post strings  
back to you. I have never tried to send a class back and forth I  
always just sent the ID and looked up that particular Customer in the  
action.

If you can send back a class than this would definitely be more DRY  
and I will have to go back and do some code cleanup if I am told  
differently in this thread. (only been on rails for about 1.5 months  
now)

Andrew
