Using string variable to call a method

Discussion in 'Ruby' started by Bryan Richardson, Jan 7, 2008.

  1. [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
    Bryan Richardson, Jan 7, 2008
    #1
    1. Advertising

  2. 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
    Stefano Crocco, Jan 7, 2008
    #2
    1. Advertising

  3. 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-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.
    Thomas Wieczorek, Jan 7, 2008
    #3
  4. [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 <> 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-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.
    >
    >
    Daniel Finnie, Jan 8, 2008
    #4
  5. [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 <> 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 <> 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-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.
    > >
    > >

    >
    Bryan Richardson, Jan 8, 2008
    #5
  6. [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 <> 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 <> 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 <> 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-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.
    > > >
    > > >

    > >

    >
    Jason Roelofs, Jan 8, 2008
    #6
  7. [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 <> 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 <> 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 <> 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 <> 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-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.
    > > > >
    > > > >
    > > >

    > >

    >
    Bryan Richardson, Jan 8, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    4
    Views:
    423
    P.Hill
    Jun 1, 2005
  2. mcdeveloper
    Replies:
    1
    Views:
    4,174
    mcdeveloper
    Jun 13, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,281
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  4. ankur
    Replies:
    1
    Views:
    12,704
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  5. Stefan Mueller
    Replies:
    9
    Views:
    119
    Stefan Mueller
    Nov 1, 2005
Loading...

Share This Page