[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

code block which allows dynamical numbers of arguments

mrpink

4/28/2007 4:23:00 PM

Hi,
let's pretend I have a codeblock like this:

#begin
test = lambda {|c,d| @check = c if object.status == d}
test.call("GoodBoy",12)
#end

So you see if object.status is 12 @check will be set to "Goodboy".

But how can I make the block test to allow more than 2 arguments and
that any further argument will be takes as further conditional statement?

Like:

test = lambda {|c,d,e| @check = c if object.status == d or object.status
== e}
test.call("GoodBoy",24,11)

or with 4 arguments:

test = lambda {|c,d,e,f| @check = c if object.status == d or
object.status == e or object.status == f}
test.call("GoodBoy",82,31,40)

so you see what I mean?

This should just happen automatically so I wanna call test sometimes
with 2, but sometimes also with more arguments and it shall be extended
as shown above.

Is there any way in ruby to do so?

--
greets
(
)
(
/\ .-"""-. / //\\/ ,,, \//\ |/\| ,;;;;;, |/\|
//\\\;-"""-;///\ // \/ . \/ \ (| ,-_| \ | / |_-, |)
//`__\.-.-./__`\ // /.-(() ())-.\ \ (\ |) '---' (| /)
` (| |) `
jgs \) (/


one must still have chaos in oneself to be able to give birth to a
dancing star
3 Answers

brabuhr

4/28/2007 4:55:00 PM

0

On 4/28/07, anansi <kazaam@oleco.net> wrote:
> let's pretend I have a codeblock like this:
>
> #begin
> test = lambda {|c,d| @check = c if object.status == d}
> test.call("GoodBoy",12)
> #end
>
> So you see if object.status is 12 @check will be set to "Goodboy".
>
> But how can I make the block test to allow more than 2 arguments and
> that any further argument will be takes as further conditional statement?
>
> Like:
>
> test = lambda {|c,d,e| @check = c if object.status == d or object.status
> == e}
> test.call("GoodBoy",24,11)
>
> This should just happen automatically so I wanna call test sometimes
> with 2, but sometimes also with more arguments and it shall be extended
> as shown above.
>
> Is there any way in ruby to do so?

test = lambda {|c,d| @check = c if 12 == d}=> #<Proc:0xb7df9528@(irb):14>
test.call("GoodBoy",11)
=> nil
test.call("GoodBoy",12)
=> "GoodBoy"

test = lambda {|*args| @check = args[0] if 12 == args[1]}
=> #<Proc:0xb7ddb820@(irb):21>
test.call("GoodBoy",11)
=> nil
test.call("GoodBoy",12)
=> "GoodBoy"

test = lambda {|*args| @check = args[0] if args[1..-1].include? 12}
=> #<Proc:0xb7db8154@(irb):28>
test.call("GoodBoy",11)
=> nil
test.call("GoodBoy",11,13)
=> nil
test.call("GoodBoy",11,13,12)
=> "GoodBoy"

Yossef Mendelssohn

4/28/2007 4:56:00 PM

0

You'd want to use the excellent splat (*) operator for that.

lambda { |check, *statuses| @check = check if statuses.include?
(object.status) }

To get an idea of what happens, see the following:

irb(main):011:0> test = lambda { |c, *args| puts c.inspect; puts
args.inspect }
=> #<Proc:0x00044d40@(irb):11>
irb(main):012:0> test.call('test')
"test"
[]
=> nil
irb(main):013:0> test.call('test', 1)
"test"
[1]
=> nil
irb(main):014:0> test.call('test', 1,2,3,4)
"test"
[1, 2, 3, 4]
=> nil

--
-yossef


mrpink

4/28/2007 5:06:00 PM

0

thank you both :D worked like a charm

--
greets
(
)
(
/\ .-"""-. / //\\/ ,,, \//\ |/\| ,;;;;;, |/\|
//\\\;-"""-;///\ // \/ . \/ \ (| ,-_| \ | / |_-, |)
//`__\.-.-./__`\ // /.-(() ())-.\ \ (\ |) '---' (| /)
` (| |) `
jgs \) (/


one must still have chaos in oneself to be able to give birth to a
dancing star