Selecting a variable number of individual items

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/.

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'] )

···

On Thu, Sep 2, 2010 at 5:05 PM, Joe Pizzanley <pizzazjoe@yahoo.com> 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.

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/\.

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!
:slight_smile:

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

···

On Thu, Sep 2, 2010 at 8:38 PM, Joe Pizzanley <pizzazjoe@yahoo.com> wrote: