# Selecting a variable number of individual items

**URL:** <https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340>\
**Category:** Ruby\
**Created:** [September 2, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340 "2010-09-02T18:05:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [September 2, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340/1 "2010-09-02T18:05:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [September 2, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340/2 "2010-09-02T19:35:55Z")

</div>

On Thu, Sep 2, 2010 at 5:05 PM, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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’] )

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [September 2, 2010, 9:38pm UTC](https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340/3 "2010-09-02T21:38:33Z")

</div>

Colin B. 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.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [September 2, 2010, 10:12pm UTC](https://www.ruby-forum.com/t/selecting-a-variable-number-of-individual-items/194340/4 "2010-09-02T22:12:51Z")

</div>

On Thu, Sep 2, 2010 at 8:38 PM, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Colin B. 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](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
