A proper Ruby way to call a procedure by name

Discussion in 'Ruby' started by John Small, Dec 14, 2008.

  1. John Small

    John Small Guest

    I know how to do this using a case statement but I want to know if
    there's a more Rubyesque way of doing things.

    I'm screen scraping from a lot of different sources. They all have much
    same data but it's presented in different ways with different access
    methods. I use scRubyt to do the scraping and define most of the setup
    for each way of presenting things in hashes which are read in from yaml
    files. That way I can nearly DRY everything up into a few simple Ruby
    routines with all the differences between the websites extracted into
    data files.

    But there's one area I'm not satisfied with because some cases are so
    different from the others that I have to write special routines just
    for them. That's ok and I can name the routines to be used for those
    cases in the yaml file. Then when the yaml config file is read in I can
    use a switch statement to tell me how to process the data from each site
    along the lines of

    result = case value
    when name1_from_yaml: result1(param1,param2)
    when name2_from_yaml: result2(param1,param2)
    etc
    end

    which is fine and dandy but what I really want to do is to be able to
    put the name of the function to call into the yaml data file and then
    somehow get to

    result = function_named_in_yaml_file(param1,param2)

    How do I do this in Ruby? In other words I want a string or symbol to
    invoke a procedure without going via a case statement.

    Ta

    John Small
    --
    Posted via http://www.ruby-forum.com/.
    John Small, Dec 14, 2008
    #1
    1. Advertising

  2. John Small

    Tim Hunter Guest

    John Small wrote:
    > I know how to do this using a case statement but I want to know if
    > there's a more Rubyesque way of doing things.
    >
    > I'm screen scraping from a lot of different sources. They all have much
    > same data but it's presented in different ways with different access
    > methods. I use scRubyt to do the scraping and define most of the setup
    > for each way of presenting things in hashes which are read in from yaml
    > files. That way I can nearly DRY everything up into a few simple Ruby
    > routines with all the differences between the websites extracted into
    > data files.
    >
    > But there's one area I'm not satisfied with because some cases are so
    > different from the others that I have to write special routines just
    > for them. That's ok and I can name the routines to be used for those
    > cases in the yaml file. Then when the yaml config file is read in I can
    > use a switch statement to tell me how to process the data from each site
    > along the lines of
    >
    > result = case value
    > when name1_from_yaml: result1(param1,param2)
    > when name2_from_yaml: result2(param1,param2)
    > etc
    > end
    >
    > which is fine and dandy but what I really want to do is to be able to
    > put the name of the function to call into the yaml data file and then
    > somehow get to
    >
    > result = function_named_in_yaml_file(param1,param2)
    >
    > How do I do this in Ruby? In other words I want a string or symbol to
    > invoke a procedure without going via a case statement.
    >
    > Ta
    >
    > John Small


    ri Object#__send__

    obj.__send__(method_name, arg1, arg2...)

    --
    RMagick: http://rmagick.rubyforge.org/
    Tim Hunter, Dec 14, 2008
    #2
    1. Advertising

  3. John Small

    John Small Guest

    Tim

    OK, thanks for that. I knew it had to be simple

    >> John Small

    >
    > ri Object#__send__
    >
    > obj.__send__(method_name, arg1, arg2...)


    --
    Posted via http://www.ruby-forum.com/.
    John Small, Dec 15, 2008
    #3
    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. Mike P
    Replies:
    0
    Views:
    3,280
    Mike P
    Jun 19, 2006
  2. Kurt M. Dresner

    proper name for one who uses ruby

    Kurt M. Dresner, Aug 23, 2003, in forum: Ruby
    Replies:
    3
    Views:
    89
    Mauricio Fernández
    Aug 23, 2003
  3. Hal Fulton
    Replies:
    1
    Views:
    154
    Gregory Brown
    Nov 5, 2009
  4. Bodi
    Replies:
    1
    Views:
    272
    Lasse Reichstein Nielsen
    Nov 18, 2003
  5. root
    Replies:
    2
    Views:
    556
    valtih1978
    Jul 12, 2012
Loading...

Share This Page