How to create a function with an unknown number of variables

3 views
Skip to first unread message

AR

unread,
Dec 10, 2009, 4:18:20 PM12/10/09
to Watir General
Basic application:
I currently have a function in almost every script that is screaming
to be stripped out and put into a global helper.rb file. At the end
of a series of tests, it sends me an email and parses the errors I've
collected stored in the array $errors. This works well, except that
some tests may store 4 items per block in the array, while others may
only store 2 or 3.

I know that I've seen other functions defined where the author was
able to use something like:
def my_function(condition1, condition2, ...)
and therefore collect as many variable as were supplied, no matter
what the limit, but I'm not sure what that is called so Google isn't
helping.

I know this is probably something mundane and simple that I am
overlooking, but I've spent enough time wondering that it is time to
ask.

As for code, I'm currently using the stream method to send me an error
digest. I've marked the line that needs to change on a test by test
basis -- all of the other items I can easily parametrize once I tackle
this problem.

CATCHING THE ERROR
begin
assert_match(expected, actual)
log_success("Expected matches actual") #custom function
rescue
log_failure("Expected does not match actual) #custom function
$errors << [expected, actual]
end #begin


THE EMAIL
def email_errors
int = 0
Net::SMTP.start("##.##.##.##") do |smtp|
smtp.open_message_stream('Automated_Test@#####.com',
'####@#####.com') do |stream|
stream.puts "Subject: ########## Errors"
stream.puts "The following whatevers failed to do the thing
you told them to:"

$errors.each do |exp, act| # ** this is the section that
needs to change on a test by test basis **
int = int + 1
stream.puts ""
stream.puts "#{int}\tCondition:#{exp}"
stream.puts "\tExpected Condition:#{act}"
stream.puts ""
end #$errors
end #smtp
smtp.finish #close the smtp connection
end #Net
end #email_errors

Bill Agee

unread,
Dec 10, 2009, 4:39:59 PM12/10/09
to watir-...@googlegroups.com
As for what it's called, various Ruby guides use the name "variable-length argument lists".  There's some good info online:

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Variable_Length_Argument_List.2C_Asterisk_Operator

http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html  (skip down to the "Variable-Length Argument Lists" section)


The O'Reilly Ruby Programming Language book also has info on the same thing.  It's a great read if you're interested in topics like this.

Thanks
Bill




--
You received this message because you are subscribed to the Google Groups "Watir General" group.
To post to this group, send email to watir-...@googlegroups.com
Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to watir-genera...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/watir-general

AR

unread,
Dec 10, 2009, 5:06:57 PM12/10/09
to Watir General
That's great - thanks Bill! I picked up Everyday Scripting with Ruby
a while back to supplement and see how other people were using it
practically, but it wouldn't hurt to have O'Reilly on hand as well.

Thanks again,
Adam

On Dec 10, 3:39 pm, Bill Agee <billa...@gmail.com> wrote:
> As for what it's called, various Ruby guides use the name "variable-length
> argument lists".  There's some good info online:
>
> http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Var...
>
> http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html (skip down
> to the "Variable-Length Argument Lists" section)
>
> The O'Reilly Ruby Programming Language book also has info on the same
> thing.  It's a great read if you're interested in topics like this.
>
> Thanks
> Bill
>
Reply all
Reply to author
Forward
0 new messages