Inheritance and Modules
Constants and Namespaces
Modules can group constants and classes under a qualified name.
Constants and Namespaces
constants_namespaces.rb
module Store
TAX_PERCENT = 8
class Calculator
def self.total(subtotal)
subtotal + subtotal * TAX_PERCENT / 100
end
end
end
subtotal =
total = Store::Calculator.total(subtotal)
puts "tax_percent=#{Store::TAX_PERCENT}"
puts "subtotal=#{subtotal}"
puts "total=#{total}"
module Store
TAX_PERCENT = 8
class Calculator
def self.total(subtotal)
subtotal + subtotal * TAX_PERCENT / 100
end
end
end
subtotal =
total = Store::Calculator.total(subtotal)
puts "tax_percent=#{Store::TAX_PERCENT}"
puts "subtotal=#{subtotal}"
puts "total=#{total}"
module Store
TAX_PERCENT = 8
class Calculator
def self.total(subtotal)
subtotal + subtotal * TAX_PERCENT / 100
end
end
end
subtotal =
total = Store::Calculator.total(subtotal)
puts "tax_percent=#{Store::TAX_PERCENT}"
puts "subtotal=#{subtotal}"
puts "total=#{total}"
namespace
A module name can act as a namespace for constants and classes that belong together.