I recently saw 2 approaches how to create a class using structs in Ruby:
Customer = Struct.new(:name, :address) do
class Customer < Struct.new(:name, :address)
Ruby actually has several scopes:
# scope one, opened with `module` keyword module ... # scope two, opened with `class` keyword class ... end end
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.