PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Using string variable to call a method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using string variable to call a method

Réponse
 
LinkBack Outils de la discussion
Vieux 07/01/2008, 21h14   #1
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using string variable to call a method

[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

  Réponse avec citation
Vieux 07/01/2008, 21h27   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

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

  Réponse avec citation
Vieux 07/01/2008, 21h47   #3
Thomas Wieczorek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

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...by-talk/192513
(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.

  Réponse avec citation
Vieux 08/01/2008, 00h25   #4
Daniel Finnie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

[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...by-talk/192513
> (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.
>
>


  Réponse avec citation
Vieux 08/01/2008, 22h25   #5
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

[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...by-talk/192513
> > (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.
> >
> >

>


  Réponse avec citation
Vieux 08/01/2008, 22h47   #6
Jason Roelofs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

[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...by-talk/192513
> > > (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.
> > >
> > >

> >

>


  Réponse avec citation
Vieux 08/01/2008, 23h38   #7
Bryan Richardson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using string variable to call a method

[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...by-talk/192513
> > > > (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.
> > > >
> > > >
> > >

> >

>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16408 seconds with 15 queries