||= is a common Ruby idiom: it assigns the value only if it is not already set. The effect is the same as code like
if some_variable == nil
some_variable = some_value
end
or
some_variable= some_value unless some_variable
===, when not overridden, compares two objects for identity. In the case of Hash === args.last
, Hash (which is an object of type Class) is checking to see if it matches the class of the last item in the args Array. The code is making use of the apparent fact that the implementation of Class#=== forces a check on the class of the compared object.
It doesn't work the other way around, for example:
a = [{}]
Hash === a.last #=> true
a.last === Hash #=> false
The trailing arguments to a method may be supplied as the contents of a hash without needing to provide the {}
So you can do this:
def hello(arg1, arg2, arg3)
puts [arg1.class, arg2.class, arg3.class].join(',')
end
hello 1,2,3 #=> Fixnum,Fixnum,Fixnum
hello :a, "b", :c => 1, :d => 99 #=> Symbol,String,Hash
It's often used to provide a variable-length list of optional parameters to the function.
Are you sure you transcribed the original code precisely, btw? In order to get an array of arguments, you would normally add an * to the argument as declared, otherwise args would have to be input as an array, whcih would rather defeat the object.
def add_spec_path_to(*args) # now args is an array
args << {} unless Hash === args.last # if trailing arguments cannot be
# interpreted as a Hash, add an empty
# Hash here so that following code will
# not fail
args.last[:spec_path] ||= caller(0)[2] # Set the spec_path option if it's not
# already set
end
EDIT: Expanding further on the *args thing, try this:
def x(*args)
puts args.join(',')
puts args.map{|a| a.class }.join(',')
end
x 1,2,:a=>5,:b=>6
1,2,a5b6
Fixnum,Fixnum,Hash
... using *args causes args to be presented to the method as an array. If I don't use the *, like this, for example:
def y(args)
puts args.join(',')
puts args.map{|a| a.class }.join(',')
end
... then args has to be an array before I call the method, or I'll get an "ArgumentError: wrong number of arguments" for anything but one thing passed. So it has to look like this:
y [1,2,{:c=>3,:d=>4}]
...with the Hash explicitly created with {}. And it's ugly.
All the above tested with MRI 1.8.6, btw.