# Ruby Basic

**URL:** <https://www.ruby-forum.com/t/ruby-basic/190485>\
**Category:** Ruby\
**Created:** [June 21, 2010, 12:52pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485 "2010-06-21T12:52:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 21, 2010, 12:52pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485/1 "2010-06-21T12:52:26Z")

</div>

Hi All,

def one  
puts “hi”  
end

Once i call one --\> i can able to get a output.

QUESTION:

I have a array method\_list=[“one”]

How can I convert “one” to method ?

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 21, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485/2 "2010-06-21T13:00:00Z")

</div>

Raveendran .P wrote:

> Hi All,
> 
> def one  
> puts “hi”  
> end
> 
> Once i call one --\> i can able to get a output.
> 
> QUESTION:
> 
> I have a array method\_list=[“one”]
> 
> How can I convert “one” to method ?  
> Tried and Got Solution

Solution:

eval(“one”)

Thanks

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 21, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485/3 "2010-06-21T13:04:24Z")

</div>

On Mon, Jun 21, 2010 at 1:00 PM, Raveendran .P [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > QUESTION:
> > 
> > I have a array method\_list=[“one”]
> > 
> > How can I convert “one” to method ?  
> > Tried and Got Solution
> 
> Solution:
> 
> eval(“one”)

I’d rather use send:

irb(main):001:0\> def one  
irb(main):002:1\> “hi”  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> list = %w{one}  
=\> [“one”]  
irb(main):005:0\> send(list.first)  
=\> “hi”

Jesus.

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 21, 2010, 2:11pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485/4 "2010-06-21T14:11:18Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> > Solution:
> > 
> > eval(“one”)
> 
> I’d rather use send:
> 
> irb(main):001:0\> def one  
> irb(main):002:1\> “hi”  
> irb(main):003:1\> end  
> =\> nil  
> irb(main):004:0\> list = %w{one}  
> =\> [“one”]  
> irb(main):005:0\> send(list.first)  
> =\> “hi”

There are some other options too.

(1) a (bound) Method object, which includes both the receiver and the  
method.

class Person  
def initialize(name)  
@name = name  
end  
def greet  
puts “hi I’m #{@name}”  
end  
end

fred = Person.new(“Fred”)  
m = fred.method(:greet)

m.call

# compare JesÃºs’ suggestion: fred.send(:greet)

(2) use a lambda instead of a method

m = lambda { puts “Hello” }  
m.call

# This also has access to surrounding local variables (closure)

c = 0  
m = lambda { c += 1; puts “Call number #{c}” }  
m.call  
m.call  
m.call

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 22, 2010, 10:01am UTC](https://www.ruby-forum.com/t/ruby-basic/190485/5 "2010-06-22T10:01:41Z")

</div>

Hi Josh,Brain and Galan,

Thanks for your help.

Regards  
Raveendran P

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![jazzezravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jazzezravi](https://www.ruby-forum.com/u/jazzezravi)\
**Post date:** [June 21, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/ruby-basic/190485/6 "2010-06-21T16:18:47Z")

</div>

On Mon, Jun 21, 2010 at 5:52 AM, Raveendran .P [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a array method\_list=[“one”]
> 
> ## How can I convert “one” to method ?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

In general, you can convert a string / symbol to the method it is maning  
by  
using the method “method”  
[http://ruby-doc.org/core/classes/Object.html#M000336](http://ruby-doc.org/core/classes/Object.html#M000336)

def one() “hi” end  
def two() “hello” end  
def three() “hola” end

method\_names = private\_methods(false).reject { |name| name.size \> 5 }  
method\_names # =\> [“three”, “two”, “one”]

method\_list = method\_names.map { |name| method name }  
method\_list # =\> [#\<Method: Object#three\>, #\<Method: Object#two\>,  
#\<Method:  
Object#one\>]

method\_results = method\_list.map { |meth| meth.call }  
method\_results # =\> [“hola”, “hello”, “hi”]
