Methods
Public Instance methods
Easy access to an object’s "special" class, otherwise known as it’s meta class or singleton class.
[ show source ]
# File lib/facets/core/kernel/metaclass.rb, line 7 def metaclass (class << self; self; end) end
Like super but skips to a specific ancestor module or class.
class A def x ; 1 ; end end class B < A def x ; 2 ; end end class C < B def x ; superior(A) ; end end C.new.x #=> 1
[ show source ]
# File lib/facets/core/kernel/superior.rb, line 20 def superior(klass=self.class.superclass, *args, &blk) unless self.class.ancestors.include?(klass) raise ArgumentError end called = /\`([^\']+)\'/.match(caller(1).first)[1].to_sym klass.instance_method(called).bind(self).call(*args,&blk) end
Boolean conversion for not being nil or false. Other classes may redefine this to suite the particular need.
"abc".to_b #=> true true.to_b #=> true false.to_b #=> false nil.to_b #=> false
[ show source ]
# File lib/facets/core/kernel/to_b.rb, line 13 def to_b self ? true : false end
[ show source ]
# File lib/facets/core/kernel/to_bool.rb, line 3 def to_bool return true end
Tests to see if something has value. An object is considered to have value if it is not nil? and if it responds to empty?, i snot.
[ show source ]
# File lib/facets/core/kernel/val.rb, line 8 def val? return false if nil? return false if empty? if respond_to?(:empty?) true end
Takes a hash and creates (singleton) attr_accessors for each key.
with_accessor { :x => 1, :y => 2 } @x #=> 1 @y #=> 2 self.x = 3 self.y = 4 self.x #=> 3 self.y #=> 4
[ show source ]
# File lib/facets/core/kernel/with_accessor.rb, line 43 def with_accessor(h) (class << self ; self ; end).send( :attr_accessor, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end
Takes a hash and creates (singleton) attr_readers for each key.
with_reader { :x => 1, :y => 2 } @x #=> 1 @y #=> 2 self.x #=> 1 self.y #=> 2
[ show source ]
# File lib/facets/core/kernel/with_accessor.rb, line 12 def with_reader(h) (class << self ; self ; end).send( :attr_reader, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end
Takes a hash and creates (singleton) attr_writers for each key.
with_writer { :x => 1, :y => 2 } @x #=> 1 @y #=> 2 self.x = 3 self.y = 4 @x #=> 3 @y #=> 4
[ show source ]
# File lib/facets/core/kernel/with_accessor.rb, line 27 def with_writer(h) (class << self ; self ; end).send( :attr_writer, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end