[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using string variable to call a method

Bryan Richardson

1/7/2008 9:14:00 PM

[Note: parts of this message were removed to make it a legal post.]

Hello all,

How can I use a string variable as part of a method name I'm calling? For
example say I want to call the method say_hello using the following:

def say_hello
puts "Hello!"
end

str = "hello"

say_????

Thanks in advance!! -- BTR

6 Answers

Stefano Crocco

1/7/2008 9:27:00 PM

0

Alle luned=EC 7 gennaio 2008, Bryan Richardson ha scritto:
> Hello all,
>
> How can I use a string variable as part of a method name I'm calling? For
> example say I want to call the method say_hello using the following:
>
> def say_hello
> puts "Hello!"
> end
>
> str =3D "hello"
>
> say_????
>
> Thanks in advance!! -- BTR

send "say_#{str}"

Stefano

Thomas Wieczorek

1/7/2008 9:47:00 PM

0

You can use: eval, instance_eval, class_eval, module_eval oder send
The different evals are described here:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
(click on the "N" to read the next message in the thread)
send is as far as I understand it, used to send messages to objects.
Keep in mind that method calls are messages send to objects.

Daniel Finnie

1/8/2008 12:26:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

In general, the send method is preferred over evalling a string when
possible.

In addition to send "say_#{str}", you can do method("say_#{str}".to_sym)
call.

Dan


On 1/7/08, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:
>
> You can use: eval, instance_eval, class_eval, module_eval oder send
> The different evals are described here:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> (click on the "N" to read the next message in the thread)
> send is as far as I understand it, used to send messages to objects.
> Keep in mind that method calls are messages send to objects.
>
>

Bryan Richardson

1/8/2008 10:26:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Along these same lines, is it possible to do a require and include
dynamically, getting the name of the file to require and module to include
from a string?

On Jan 7, 2008 5:25 PM, Daniel Finnie <danfinnie@optonline.net> wrote:

> In general, the send method is preferred over evalling a string when
> possible.
>
> In addition to send "say_#{str}", you can do method("say_#{str}".to_sym)
> .call.
>
> Dan
>
>
> On 1/7/08, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:
> >
> > You can use: eval, instance_eval, class_eval, module_eval oder send
> > The different evals are described here:
> > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> > (click on the "N" to read the next message in the thread)
> > send is as far as I understand it, used to send messages to objects.
> > Keep in mind that method calls are messages send to objects.
> >
> >
>

Jason Roelofs

1/8/2008 10:47:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

require is obvious, the method takes a string already.

Including a module, I think you can do:

ClassName.send(:include, Module.const_get("ModuleName"))

or if you're working with an instance variable, replace :include with
:extend.

Jason

On Jan 8, 2008 5:25 PM, Bryan Richardson <btricha@gmail.com> wrote:

> Along these same lines, is it possible to do a require and include
> dynamically, getting the name of the file to require and module to include
> from a string?
>
> On Jan 7, 2008 5:25 PM, Daniel Finnie <danfinnie@optonline.net> wrote:
>
> > In general, the send method is preferred over evalling a string when
> > possible.
> >
> > In addition to send "say_#{str}", you can do method("say_#{str}".to_sym)
> > .call.
> >
> > Dan
> >
> >
> > On 1/7/08, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:
> > >
> > > You can use: eval, instance_eval, class_eval, module_eval oder send
> > > The different evals are described here:
> > > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> > > (click on the "N" to read the next message in the thread)
> > > send is as far as I understand it, used to send messages to objects.
> > > Keep in mind that method calls are messages send to objects.
> > >
> > >
> >
>

Bryan Richardson

1/8/2008 11:38:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

send :include, Module.const_get(ARGV[1]) worked. Thanks!!

On Jan 8, 2008 3:47 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

> require is obvious, the method takes a string already.
>
> Including a module, I think you can do:
>
> ClassName.send(:include, Module.const_get("ModuleName"))
>
> or if you're working with an instance variable, replace :include with
> :extend.
>
> Jason
>
> On Jan 8, 2008 5:25 PM, Bryan Richardson <btricha@gmail.com> wrote:
>
> > Along these same lines, is it possible to do a require and include
> > dynamically, getting the name of the file to require and module to
> include
> > from a string?
> >
> > On Jan 7, 2008 5:25 PM, Daniel Finnie <danfinnie@optonline.net> wrote:
> >
> > > In general, the send method is preferred over evalling a string when
> > > possible.
> > >
> > > In addition to send "say_#{str}", you can do
> method("say_#{str}".to_sym)
> > > .call.
> > >
> > > Dan
> > >
> > >
> > > On 1/7/08, Thomas Wieczorek <wieczo.yo@googlemail.com> wrote:
> > > >
> > > > You can use: eval, instance_eval, class_eval, module_eval oder send
> > > > The different evals are described here:
> > > > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> > > > (click on the "N" to read the next message in the thread)
> > > > send is as far as I understand it, used to send messages to objects.
> > > > Keep in mind that method calls are messages send to objects.
> > > >
> > > >
> > >
> >
>