SvenDittmer SvenDittmer - 13 days ago 7
Ruby Question

What's the difference between the different approaches to creating classes from structs?

I recently saw 2 approaches how to create a class using structs in Ruby:

Customer = Struct.new(:name, :address) do
# ...
end

class Customer < Struct.new(:name, :address)
# ...
end


What's the difference between these approaches?

Answer

Ruby actually has several scopes:

# scope one, opened with `module` keyword
module ...
  # scope two, opened with `class` keyword
  class ...
  end
end

module, class some of them.

When you're using the first example you able to share a scope to access the f variable, it is very handy in some circumstances:

=> f = 1
=> 1
=> Customer = Struct.new(:a) do
=>  puts f
=> end
=> 1
=> #<Customer:0x005561498351f8>

With the second example, you can't access f variable variable:

=> f = 1
=> class Customer < Struct.new(:a)
=>  puts f
=> end
#> NameError: undefined local variable or method `f' for Customer:Class

There is also a difference in the ancestors chain - see @AndreyDeineko's answer.