Extending to an existing case menu

Discussion in 'Ruby' started by Marc Heiler, Nov 24, 2007.

  1. Marc Heiler

    Marc Heiler Guest

    user_input = $stdin.gets.chomp

    case user_input
    when 'x'
    puts 'this is a test'
    end

    # So far so fine. Now I want to add something like

    when 'y'
    puts 'yay, a new test'
    end

    What would be the simplest or most elegant way?
    A hash with key -> action ?

    Only problem is that the action could be invoking a method or creating
    an object etc... and i dont want to use proc or lambda's at all.

    I would just like to "extend" the case menu somehow
    (I need to split a huge case menu into "core" modules, and
    stuff which a user wants to add to it)
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Nov 24, 2007
    #1
    1. Advertising

  2. Marc Heiler

    Phrogz Guest

    On Nov 24, 4:23 am, Marc Heiler <> wrote:
    > user_input = $stdin.gets.chomp
    >
    > case user_input
    > when 'x'
    > puts 'this is a test'
    > end
    >
    > # So far so fine. Now I want to add something like
    >
    > when 'y'
    > puts 'yay, a new test'
    > end
    >
    > What would be the simplest or most elegant way?
    > A hash with key -> action ?


    The simplest, most elegant way, would be to just type it into the same
    case code :)

    If you want to be able to define the (arbitrary) results of a
    comparison separate from the comparison itself, I think you need to
    use blocks/procs. A Hash would certainly be a good way to store this,
    assuming a simple case-like #=== comparison is all you need.

    However, the syntax of defining a Hash of procs may not be the most
    ideal, depending on your scenario. I might use a method wrapper like
    so:

    class ConditionMapper
    def initialize
    @conditions = {}
    end
    def when( *conditions, &action )
    conditions.each{ |condition| @conditions[ condition ] = action }
    end
    def check( obj )
    @conditions.each{ |condition,action|
    if condition === obj
    return action[ obj ]
    end
    }
    end
    end

    english_match = ConditionMapper.new
    english_match.when( 1..5 ){ |x|
    puts "#{x} is between one and five"
    }

    english_match.when( 7, 42 ){ |x|
    puts "#{x} is a cool number"
    }


    english_match.check( 4 )
    #=> 4 is between one and five

    english_match.check( 42 )
    #=> 42 is a cool number



    > Only problem is that the action could be invoking a method or creating
    > an object etc... and i dont want to use proc or lambda's at all.


    Why not?
     
    Phrogz, Nov 24, 2007
    #2
    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. nrm
    Replies:
    3
    Views:
    535
  2. =?iso-8859-1?q?Fran=E7ois_Beretti?=

    Extending an existing XML format

    =?iso-8859-1?q?Fran=E7ois_Beretti?=, May 18, 2007, in forum: XML
    Replies:
    1
    Views:
    375
    Joe Kesselman
    May 19, 2007
  3. BlueFlash
    Replies:
    2
    Views:
    470
    John Ladasky
    Aug 26, 2009
  4. Replies:
    0
    Views:
    192
  5. Andreas M.
    Replies:
    0
    Views:
    71
    Andreas M.
    May 5, 2008
Loading...

Share This Page