- descendents
- descendents
- methodize
- methodize
- pathize
- pathize
- prepend
- prepend
- subclasses
- subclasses
- to_proc
- to_proc
List all descedents of this class.
class X ; end class A < X; end class B < X; end X.descendents #=> [A,B]
NOTE: This is a intesive operation. Do not expect it to be super fast.
[ show source ]
# File lib/core/facets/class/descendents.rb, line 13 def descendents subclass = [] ObjectSpace.each_object( Class ) do |c| if c.ancestors.include?( self ) and self != c subclass << c end end return subclass end
List all descedents of this class.
class X ; end class A < X; end class B < X; end X.descendents #=> [A,B]
NOTE: This is a intesive operation. Do not expect it to be super fast.
[ show source ]
# File lib/core/facets/class/descendents.rb, line 13 def descendents subclass = [] ObjectSpace.each_object( Class ) do |c| if c.ancestors.include?( self ) and self != c subclass << c end end return subclass end
Translate a class name to a suitable method name.
My::CoolClass.methodize => "my__cool_class"
[ show source ]
# File lib/core/facets/class/methodize.rb, line 9 def methodize name.methodize end
Translate a class name to a suitable method name.
My::CoolClass.methodize => "my__cool_class"
[ show source ]
# File lib/core/facets/class/methodize.rb, line 9 def methodize name.methodize end
Converts a class name to a unix path.
My::CoolClass.pathize #=> "my/cool_class"
[ show source ]
# File lib/core/facets/class/pathize.rb, line 9 def pathize name.pathize end
Converts a class name to a unix path.
My::CoolClass.pathize #=> "my/cool_class"
[ show source ]
# File lib/core/facets/class/pathize.rb, line 9 def pathize name.pathize end
Prepend an "aspect module" to a class.
class Firetruck
def put_out_fire(option)
"Put out #{option}"
end
end
module FastFiretruck
def put_out_fire(option)
super("very #{option}!")
end
end
Firetruck.prepend(FastFiretruck)
ft = Firetruck.new
ft.put_out_fire('fast') #=> "Put out very fast!"
Implementation of this method has some limitations, in that it works by overriding new and allocate.
CREDIT Trans
TODO: Perhaps rename this to preallocate, b/c of
the way it works. It is not really a clean
prepend, like that of Module#prepend.
[ show source ]
# File lib/core/facets/class/prepend.rb, line 31 def prepend( aspect ) _new = method(:new) _allocate = method(:allocate) (class << self; self; end).class_eval do define_method(:new) do |*args| o = _new.call(*args) o.extend aspect o end define_method(:allocate) do |*args| o = _allocate.call(*args) o.extend aspect o end end end
Prepend an "aspect module" to a class.
class Firetruck
def put_out_fire(option)
"Put out #{option}"
end
end
module FastFiretruck
def put_out_fire(option)
super("very #{option}!")
end
end
Firetruck.prepend(FastFiretruck)
ft = Firetruck.new
ft.put_out_fire('fast') #=> "Put out very fast!"
Implementation of this method has some limitations, in that it works by overriding new and allocate.
CREDIT Trans
TODO: Perhaps rename this to preallocate, b/c of
the way it works. It is not really a clean
prepend, like that of Module#prepend.
[ show source ]
# File lib/core/facets/class/prepend.rb, line 31 def prepend( aspect ) _new = method(:new) _allocate = method(:allocate) (class << self; self; end).class_eval do define_method(:new) do |*args| o = _new.call(*args) o.extend aspect o end define_method(:allocate) do |*args| o = _allocate.call(*args) o.extend aspect o end end end
Alias for descendents
Alias for descendents
Convert instatiation of a class into a Proc.
class Person
def initialize(name)
@name = name
end
def inspect
@name.to_str
end
end
%w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
CREDIT: Daniel Schierbeck
[ show source ]
# File lib/core/facets/class/to_proc.rb, line 19 def to_proc proc{|*args| new(*args)} end
Convert instatiation of a class into a Proc.
class Person
def initialize(name)
@name = name
end
def inspect
@name.to_str
end
end
%w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
CREDIT: Daniel Schierbeck
[ show source ]
# File lib/core/facets/class/to_proc.rb, line 19 def to_proc proc{|*args| new(*args)} end