I used "" instead of '' in my example for yield. A visitor commented that a single quote is better than double quote.
"Not to be picky either, but does the above ruby code need single
quotes?
Around 'Calling GetValue2' and puts ahead of the call to GetValue2?"
That is correct, but I have years of C++, Java, C# experience to undo! :)
What's in double quotes is evaluated in Ruby. However, what's in single quote is not.
If you don't have anything to
expand, use single quote.
I said to myself, "wonder what the price I pay for using double quotes?" When I got
into C++, Java, C#, it took me
a while to measure the time and generate a report. Ruby, as sweet it is, comes with
a nice benchmark library. So, I
decided to use that to find out what is the penalty for double quotes usage. Here
is what I wrote:
require
'benchmark'
include Benchmark
def dummy(str)
str.length
end
TIMES_TO_RUN = 10000
x = 1
bm(10) do |timeIt|
timeIt.report("double:")
{ TIMES_TO_RUN.times { dummy("test#{x}")
}}
timeIt.report("single:")
{ TIMES_TO_RUN.times { dummy('test#{x}') }}
end
The dummy method returns the length of the parameter. I call dummy 10K times.
(I could not find any time difference when I called it a 1000 times, so I bumped it
to 10000).
I got the following output on my machine when I ran the above code (time
in seconds):
user system
total real
double: 0.030000 0.000000 0.030000
( 0.030000)
single: 0.010000 0.000000 0.010000
( 0.010000)
User CPU time, System CPU time, total of the two, and real time elapsed.