selecting a variable number of individual items

Discussion in 'Ruby' started by Joe Pizzanley, Sep 2, 2010.

  1. I have a select field where I want to select a variable number of
    individual items. How would I set up a def to handle this?

    for example, one time I would select apples. Another time I would select
    pears, plums and grapes.
    I think that the code line: $ie.select_list:)name,
    'FRUITS').select_value('plums') would vary in value and also in times
    called, but that's as far as I can figure out.

    (I can't call myself a newbie because I've been studying Ruby a long
    time. I'm just not what you would call 'talented' with programming. I
    guess I'm a ruby learner.)

    Cheers, Joe
    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Pizzanley, Sep 2, 2010
    #1
    1. Advertising

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

    On Thu, Sep 2, 2010 at 5:05 PM, Joe Pizzanley <> wrote:

    > I have a select field where I want to select a variable number of
    > individual items. How would I set up a def to handle this?
    >
    > for example, one time I would select apples. Another time I would select
    > pears, plums and grapes.
    > I think that the code line: $ie.select_list:)name,
    > 'FRUITS').select_value('plums') would vary in value and also in times
    > called, but that's as far as I can figure out.
    >
    > (I can't call myself a newbie because I've been studying Ruby a long time.


    I'm a bit hesitant to suggest this because you've been studying Ruby a long
    time, and because I don't think I understand sufficiently what you want to
    do. Do you want to do something like these:

    class VarArgsExample
    def select_example_using_variable_args( *args )
    # now process the arguments to the method, for example
    args.each do |v|
    # do something with v
    end
    # maybe more processing
    end

    def select_example_using_array_arg( ary )
    # a similar idea works for a hash argument
    ary = [ary] unless ary.kind_of?( Array )
    # now process the array values, for example
    ary.each do |v|
    # do something with v
    end
    # maybe more processing
    end
    end

    obj = VarArgsExample.new

    obj.select_example_using_variable_args( 'apples' )
    obj.select_example_using_variable_args( 'pears', 'plums', 'grapes' )

    obj.select_example_using_array_arg( 'apples' )
    obj.select_example_using_array_arg( ['apples'] )
    obj.select_example_using_array_arg( ['pears', 'plums', 'grapes'] )
     
    Colin Bartlett, Sep 2, 2010
    #2
    1. Advertising

  3. Colin Bartlett wrote:
    > I'm a bit hesitant to suggest this because you've been studying Ruby a
    > long time,


    you're assuming that I've gotten good at it. It's still confusing to me
    in many spots.

    > and because I don't think I understand sufficiently what you want to do.


    the second half of your sentence is proof that I haven't gotten good at
    it.


    However your solution gave me enough to go on. I can push the arguments
    into an array and then iterate through selecting whatever is in the
    array.


    thanks for extending help Colin! Much appreciated.

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Pizzanley, Sep 2, 2010
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Thu, Sep 2, 2010 at 8:38 PM, Joe Pizzanley <> wrote:

    > Colin Bartlett wrote:
    > > I'm a bit hesitant to suggest this because you've been studying Ruby a
    > > long time,

    > you're assuming that I've gotten good at it. It's still confusing to me
    > in many spots.
    >
    > > and because I don't think I understand sufficiently what you want to do.

    > the second half of your sentence is proof that I haven't gotten good at it.
    >

    Well, there is a difference between not knowing what you want to do, and
    knowing what you want to do but not be able to explain what you want to do!
    :)

    >
    > However your solution gave me enough to go on. I can push the arguments
    > into an array and then iterate through selecting whatever is in the array.
    > thanks for extending help Colin! Much appreciated.
    >

    No trouble: I wasn't sure what level to pitch any explanation at, and the
    examples were aimed at what I thought you wanted to do - it seems that I
    wasn't all that far off?

    The following is a very simplified explanation of Ruby method arguments,
    targetted at want you want to do. There's a longer (not quite up-to-date -
    Ruby 1.9 allows more method argument options) explanation here:
    http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html

    class VarArgsExample
    # example showing compulsory arguments, default arguments,
    # and variable length arguments
    def example_meth( arg0, arg1, arg2 = 222, arg3 = 333, *args )
    puts "0=#{arg0.inspect}, 1=#{arg1.inspect}," \
    " 2=#{arg2.inspect}, 3=#{arg3.inspect};" \
    " var len args= #{args.inspect};"
    "return value"
    end
    end

    obj = VarArgsExample.new

    obj.example_meth( ) rescue (p $!)
    #=> #<ArgumentError: wrong number of arguments (0 for 2)>
    obj.example_meth( 'apple' ) rescue (p $!)
    #=> #<ArgumentError: wrong number of arguments (1 for 2)>
    obj.example_meth( 'apple', 'fig' )
    #=> 0="apple", 1="fig", 2=222, 3=333; var len args=[]
    obj.example_meth( 'apple', 'fig', 'lemon' )
    #=> 0="apple", 1="fig", 2="lemon", 3=333; var len args=[]
    obj.example_meth( 'apple', 'fig', 'lemon', 'grape' )
    #=> 0="apple", 1="fig", 2="lemon", 3="grape"; var len args=[]
    obj.example_meth( 'apple', 'fig', 'lemon', 'grape', 'cherry' )
    #=> 0="apple", 1="fig", 2="lemon", 3="grape"; var len args=["cherry"]
    obj.example_meth( 'apple', 'fig', 'lemon', 'grape', 'cherry', 'blossom' )
    #=> 0="apple", 1="fig", 2="lemon", 3="grape"; var len args=["cherry",
    "blossom"]

    For what you want do you can just do:
    def select_values( *args )
    # array args holds all the arguments passed to this method;
    # and if you want to trap if no arguments were passed then
    if args.size == 0 then
    raise "select_values must have at least one argument"
    end
    end
     
    Colin Bartlett, Sep 2, 2010
    #4
    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. r3bol
    Replies:
    5
    Views:
    256
    Terry Reedy
    Nov 9, 2008
  2. W Akthar

    Inserting individual items into a datagrid

    W Akthar, Aug 10, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    103
    Bharat Biyani
    Aug 11, 2004
  3. Dunno
    Replies:
    0
    Views:
    206
    Dunno
    May 16, 2005
  4. Timothy Baron
    Replies:
    4
    Views:
    107
    (r.*n){2}
    Sep 23, 2010
  5. googler

    Accessing individual bits in a number

    googler, May 19, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    110
    googler
    May 19, 2008
Loading...

Share This Page