Module: Ramaze::CoreExtensions::Object

Defined in:
lib/ramaze/snippets/object/scope.rb,
lib/ramaze/snippets/object/pretty.rb,
lib/ramaze/snippets/object/__dir__.rb,
lib/ramaze/snippets/object/acquire.rb,
lib/ramaze/snippets/kernel/constant.rb,
lib/ramaze/snippets/object/instance_variable_defined.rb

Overview

Extensions for Object

Instance Method Summary collapse

Instance Method Details

#__DIR__(*args) ⇒ 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.expand_path(File.dirname(__FILE__))

idiom.



20
21
22
23
24
# File 'lib/ramaze/snippets/object/__dir__.rb', line 20

def __DIR__(*args)
  filename = caller[0][/^(.*):/, 1]
  dir = File.expand_path(File.dirname(filename))
  ::File.expand_path(::File.join(dir, *args.map{|a| a.to_s}))
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/*'


30
31
32
33
# File 'lib/ramaze/snippets/object/acquire.rb', line 30

def acquire(*globs)
  Ramaze.deprecated('Object#acquire', 'Ramaze::acquire')
  Ramaze.acquire(*globs)
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


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ramaze/snippets/kernel/constant.rb', line 25

def constant(const)
  const = const.to_s.dup

  if const.sub!(/^::/, '')
    base = Object
  elsif self.kind_of?(Module)
    base = self
  else
    base = self.class
  end

  const.split(/::/).inject(base){ |mod, name| mod.const_get(name) }
end

#instance_variable_defined?(variable) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ramaze/snippets/object/instance_variable_defined.rb', line 12

def instance_variable_defined?(variable)
  instance_variables.include?(variable.to_s)
end

#pretty(s = '') ⇒ Object

Returns the string that #pretty_inspect would yield



9
10
11
12
# File 'lib/ramaze/snippets/object/pretty.rb', line 9

def pretty s = ''
  PP.pp(self, s)
  s
end

#scopeObject

returns a new clean binding for this object

usage: eval 'self', object.scope  #=> returns object


11
12
13
# File 'lib/ramaze/snippets/object/scope.rb', line 11

def scope
  lambda{}
end