Module: Kernel

Defined in:
lib/rns.rb

Instance Method Summary collapse

Instance Method Details

#Rns(*imports, &block) ⇒ Object

Returns an immutable namespace of functions

  • ‘imports` can be any number of other namespaces from which to import functions or hashes specifying which functions to import.

  • The block contains the functions to be ‘def`ed for the namespace. Since namespaces are immutable, instance variable and class variable cannot be set in functions. In other words, `@var = :something` will raise an error.

Example:

StringManipulation = Rns(SomeOtherNs, AnotherNs => [:alternate_case]) do
  def crazify(str)
    "#{alternate_case str}!"
  end
end

StringManipulation.crazify("whoa") #=> "WhOa!"


22
23
24
25
26
# File 'lib/rns.rb', line 22

def Rns(*imports, &block)
  klass = Class.new(Rns::Namespace, &block)
  klass.import(imports)
  klass.freeze.send(:new).freeze
end