Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/snippets/object/__dir__.rb,
lib/snippets/object/instance_exec.rb

Overview

Stolen from Ramaze

Defined Under Namespace

Modules: InstanceExecHelper

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.


17
18
19
20
21
# File 'lib/snippets/object/__dir__.rb', line 17

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

#instance_exec(*args, &block) ⇒ Object

!> method redefined; discarding old instance_exec


4
5
6
7
8
9
10
11
12
13
# File 'lib/snippets/object/instance_exec.rb', line 4

def instance_exec(*args, &block) # !> method redefined; discarding old instance_exec
  mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
  InstanceExecHelper.module_eval{ define_method(mname, &block) }
  begin
    ret = send(mname, *args)
  ensure
    InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
  end
  ret
end