# Treating 'string'+var as method

**URL:** <https://www.ruby-forum.com/t/treating-string-var-as-method/50333>\
**Category:** Ruby\
**Created:** [December 15, 2005, 11:29am UTC](https://www.ruby-forum.com/t/treating-string-var-as-method/50333 "2005-12-15T11:29:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [December 15, 2005, 11:29am UTC](https://www.ruby-forum.com/t/treating-string-var-as-method/50333/1 "2005-12-15T11:29:29Z")

</div>

I am a new Ruby recruit (thanks to _ahem_, Rails)…  
What is the best way to do this:

- obj.‘string’+var as a method?

I am currently using:

- obj.send(‘string’+var)

…which works fine, but is this correct or bad practice?

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [December 15, 2005, 11:53am UTC](https://www.ruby-forum.com/t/treating-string-var-as-method/50333/2 "2005-12-15T11:53:40Z")

</div>

On Thu, 15 Dec 2005 10:22:47 -0000, bmgz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am a new Ruby recruit (thanks to _ahem_, Rails)…  
> What is the best way to do this:
> 
> - obj.‘string’+var as a method?
> 
> I am currently using:
> 
> - obj.send(‘string’+var)
> 
> …which works fine, but is this correct or bad practice?

I’m not sure what’s the best way, but here are a few things you could  
try:

# A hash to play with

h = Hash.new  
=\> {}  
irb(main):112:0\> suff = “ore”  
=\> “ore”

# You can use ‘eval’ but this is pretty heavy on time and space:

irb(main):113:0\> eval(“h.st#{suff}(‘k’,‘v’)”)  
=\> “v”  
irb(main):114:0\> h  
=\> {“k”=\>“v”}

# You can use send, as you did. I’d replace ‘str’+var with “str#{var}”,

but it’s not required:  
irb(main):115:0\> h.send(“st#{suff}”, ‘k’, ‘g’)  
=\> “g”  
irb(main):117:0\> h  
=\> {“k”=\>“g”}

# You can get the method and call it as a proc

irb(main):119:0\> store = h.method(“st#{suff}”)  
=\> #\<Method: Hash#store\>  
irb(main):121:0\> store.call(‘k’,‘g’)  
=\> “g”  
irb(main):122:0\> store[‘k’,‘g’] # (shortcut, maybe a bit confusing here  
though ;))  
=\> “g”

Hope that helps,  
Ross

---

<div class="post-metadata">

**Author:** ![bmgz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/439d5e/32.png) [@bmgz](https://www.ruby-forum.com/u/bmgz)\
**Post date:** [December 15, 2005, 12:05pm UTC](https://www.ruby-forum.com/t/treating-string-var-as-method/50333/3 "2005-12-15T12:05:43Z")

</div>

bmgz wrote:

> I am a new Ruby recruit (thanks to _ahem_, Rails)…  
> What is the best way to do this:
> 
> - obj.‘string’+var as a method?
> 
> I am currently using:
> 
> - obj.send(‘string’+var)
> 
> …which works fine, but is this correct or bad practice?

That’s ok. As Ross pointed out you can also use  
obj.send(“string#{var}”).  
But I would not use eval - there’s just too much that can go wrong  
security wise.

Kind regards

```
robert
```
