Types in Ruby
Conversations at work
A couple of months ago, I wrote a greedy bin packing algorithm for work. The other day, Chris decided he a needed a simpler packing algorithm, and asked me to explain how my solution worked.
“What type is files?” Chris asked as we stepped through the code.
files.each do |file| name = file size = file # Bin packing algorithm... end
“It’s most likely a hash or a two-dimensional array,” I said. “Probably a hash; though, I’m not really sure.”
Chris gave me a look that said, “How can you look at code you wrote and not even know what types of variables you used?”
Well, Ruby doesn’t really have types.
A Ruby variable holds a reference to an object of a certain type if it behaves like that type. This is commonly called duck typing, i.e. “If it walks like a duck, and it quacks like a duck, it must be a duck.”
When people are first learning Ruby, they tend to think that type and class are
files must be a hash. But
class Files def class Hash end end files = Files.new if files.class == Hash files['ruby.txt'] = 3 end
files doesn’t have a reference to a
Hash, it has a reference to a
Files object. Okay, so what about using the
class Files def instance_of?(type) type == Hash end end files = Files.new if files.instance_of?(Hash) files['ruby.txt'] = 3 end
That doesn’t work.
instance_of? suffers from the same problem as
the code is executed, it throws a error saying the
= method is missing. So
what about asking the…