[Kernel,String,Object].each do |klass|
case klass.to_s
when ‘Kernel’ :
puts ‘I’m doing stuff with object Kernel’
when ‘String’ :
puts ‘I’m doing stuff with object String’
when ‘Object’ :
puts ‘I’m doing stuff with object Object’
end
end
Sure. Drop the call to to_s() and the ’ … 's around the class
names. Case calls ===() for the condition object and Class defines
===() to do what you expect here.
[Kernel,String,Object].each do |klass|
case
when klass == Kernel :
puts ‘I’m doing stuff with object Kernel’
when klass == String :
puts ‘I’m doing stuff with object String’
when klass == Object :
puts ‘I’m doing stuff with object Object’
end
end
well, the code you have above is exactly equivalent to this
[Kernel, String, Object].each{|k| puts “I’m doing stuff with object
#{ k }”}
but i suppose your real code is quite different? my point is just that
looping over three things and selecting them each in turn is the same
thing as
pre-selecting the items to loop over. another alternative is