[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Default values for arguments

Patrick Spence

9/11/2006 7:32:00 PM

For some odd reason, the default values for some of the optional
arguments are not getting assigned, namely the _dateTime argument. As a
result, I have to add the "if blahblah.nil?" hacks below. Anyone know
what's going on here?


def saveResults(_scriptPoint, _scriptName, _testResult=self.RESULTVALUE_SUCCESS, _seqNo=nil, _dateTime=Time.now(), _comments=nil, _imagePath=nil, _imageBinary=nil)

#-- open connection with SQL Server database
@sqlConnection = SqlClient::SqlConnection.new(@dataSource,
@initialCatalog)

#-- determine next sequence number for the current logID if seqNo
parameter not passed
_seqNo = self.getMaxSeqNo() + 1 if _seqNo.nil?

#-- !!hacking starts here!!
_dateTime = Time.now() if _dateTime.nil?
_comments = "".to_s() if _comments.nil?
_testResult = SqlErrorLogger::RESULTVALUE_INFO if _testResult.nil?

sqlInsert = "INSERT INTO " "qa_loggerresults " "(" "logid, " "seqno, " "logdate, " "scriptpoint, " "scriptname, " "comments, " "testresult"

#-- append the additional column names if their values were passed
sqlInsert.concat(", screenimagepath") unless _imagePath.nil?
sqlInsert.concat(", screenimage") unless _imageBinary.nil?

#-- append a closing parenthesis
sqlInsert.concat(") ")

sqlInsert.concat( "VALUES " "(" "#{self.newLogID}, " "#{_seqNo}, " "'#{self.formatDateTime(_dateTime)}', " "'#{_scriptPoint}', " "'#{_scriptName}', " "'#{_comments}', " "#{_testResult}")

#-- append the additional column values if they're not nil
sqlInsert.concat(", '#{_imagePath}'") unless _imagePath.nil?
sqlInsert.concat(", #{_imageBinary}") unless _imageBinary.nil?

#-- append a closing parenthesis
sqlInsert.concat(")")

puts(sqlInsert)

@sqlConnection.execute(sqlInsert)

end #-- SqlErrorLogger.saveResults()

--
Posted via http://www.ruby-....

1 Answer

Paul Lutus

9/11/2006 7:58:00 PM

0

Patrick Spence wrote:

> For some odd reason, the default values for some of the optional
> arguments are not getting assigned, namely the _dateTime argument. As a
> result, I have to add the "if blahblah.nil?" hacks below. Anyone know
> what's going on here?

I just did an exhaustive test using a similar method, and I cannot reproduce
your result:

---------------------------------------

#!/usr/bin/ruby

def testMethod(a,b,c = nil,d = Time.now,e = nil,f = "f")
print a.nil?," ",b.nil?," ",c.nil?," ",d.nil?," ",e.nil?," ",f.nil?,"\n"
print a," ",b," ",c," ",d," ",e," ",f,"\n"
end

testMethod("a","b")


Output:

false false true false true false
a b nil Mon Sep 11 12:57:32 PDT 2006 nil f

So ... I have no idea. What Ruby version, what platform?

--
Paul Lutus
http://www.ara...