Module: Kernel
- Defined in:
- lib/ramaze/snippets/kernel/__dir__.rb,
lib/vendor/bacon.rb,
lib/ramaze/snippets/kernel/acquire.rb,
lib/ramaze/snippets/kernel/constant.rb,
lib/ramaze/snippets/kernel/pretty_inspect.rb
Overview
Extensions for Kernel
Instance Method Summary collapse
-
#__DIR__ ⇒ Object
This is similar to
__FILE__
and__LINE__
, and returns a String representing the directory of the current file is. -
#acquire(*globs) ⇒ Object
Require all .rb and .so files on the given globs, utilizes Dir::[].
-
#constant(const) ⇒ Object
Original from Trans (Facets 1.4.5) This is similar to Module#const_get but is accessible at all levels, and, unlike
const_get
, can handle module hierarchy. -
#pretty_inspect ⇒ Object
returns a pretty printed object as a string.
Instance Method Details
#__DIR__ ⇒ Object
This is similar to __FILE__
and __LINE__
, and returns a String representing the directory of the current file is. Unlike __FILE__
the path returned is absolute.
This method is convenience for the
File.(File.dirname(__FILE__))
idiom.
18 19 20 21 |
# File 'lib/ramaze/snippets/kernel/__dir__.rb', line 18 def __DIR__() filename = caller[0][/^(.*):/, 1] File.(File.dirname(filename)) end |
#acquire(*globs) ⇒ Object
Require all .rb and .so files on the given globs, utilizes Dir::[].
Examples:
# Given following directory structure:
# src/foo.rb
# src/bar.so
# src/foo.yaml
# src/foobar/baz.rb
# src/foobar/README
# requires all files in 'src':
acquire 'src/*'
# requires all files in 'src' recursive:
acquire 'src/**/*'
# require 'src/foo.rb' and 'src/bar.so' and 'src/foobar/baz.rb'
acquire 'src/*', 'src/foobar/*'
27 28 29 30 31 32 33 |
# File 'lib/ramaze/snippets/kernel/acquire.rb', line 27 def acquire *globs globs.flatten.each do |glob| Dir[glob].each do |file| require file if file =~ /\.(rb|so)$/ end end end |
#constant(const) ⇒ Object
Original from Trans (Facets 1.4.5) This is similar to Module#const_get but is accessible at all levels, and, unlike const_get
, can handle module hierarchy.
constant("Fixnum") # -> Fixnum
constant(:Fixnum) # -> Fixnum
constant("Process::Sys") # -> Process::Sys
constant("Regexp::MULTILINE") # -> 4
require 'test/unit'
Test.constant("Unit::Assertions") # -> Test::Unit::Assertions
Test.constant("::Test::Unit") # -> Test::Unit
21 22 23 24 25 |
# File 'lib/ramaze/snippets/kernel/constant.rb', line 21 def constant(const) const = const.to_s.dup base = const.sub!(/^::/, '') ? Object : ( self.kind_of?(Module) ? self : self.class ) const.split(/::/).inject(base){ |mod, name| mod.const_get(name) } end |
#pretty_inspect ⇒ Object
returns a pretty printed object as a string.
11 12 13 |
# File 'lib/ramaze/snippets/kernel/pretty_inspect.rb', line 11 def pretty_inspect PP.pp(self, '') end |