methods | -> | object_methods |
class | -> | __class__ |
Defines core method class as an alias of class. This allows you to
use class as your own method, without loosing the ability to determine the
object’s class.
require 'facet/object/__class__' |
||
class | -> | object_class |
Defines object_classas an alias of class. This is an alternative to
class, akin to object_id.
require 'facet/object/__class__' |
Set setter methods and/or instance vars using a hash from another object.
require 'facet/object/assign_with' class O attr_accessor :d def initialize( a, b, c, d) @a = a @@b = b $c = c d = d end end o1 = O.new(1,2,3,4) o2 = O.new(0,0,0,0) o2.assign_from { o1, '@a', '@@b', '$c', 'd' } o2.instance_eval{ @a } #=> 1 o2.instance_eval{ @@b } #=> 2 o2.instance_eval{ $c } #=> 3 02.d #=> 4
See also assign_with.
Set setter methods and/or vars using a hash (or assoc array). assign_with is a meta-programming method, which allows you to use a hash to do any kind of variable assignment and/or setting:
For example, assuming that there is an accessor defined as d:
require 'facet/object/assign_with' assign_with { '@a'=>1, '@@b'=>2, '$c'=>3, 'd'=>4 } @a #=> 1 @@b #=> 2 $c #=> 3 d #=> 4
Note that while the global variable is strictly unnecessary, it works for completeness sake.
Returns true is an object is class TrueClass or FalseClass, otherwise false.
require 'facet/object/bool?' true.bool? #=> true false.bool? #=> true nil.bool? #=> false
This is similar to +Module#const_get+ but is accessible at all levels, and, unlike const_get, can handle module hierarchy.
require 'facet/object/constant' 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
Anything that can be marshaled can be copied in totality. This is also commonly called a deep_copy.
require 'facet/object/copy' "ABC".copy #=> "ABC"
Returns true is an object is class FalseClass, otherwise false.
require 'facet/object/bool?' true.false? #=> false false.false? #=> true nil.false? #=> false
Is self included in other?
require 'facet/object/in?' 5.in?(0..10) #=> true 5.in?([0,1,2,3]) #=> false
Returns a list of methods according to symbol(s) given. Note that this method derives from ‘facet/module/methods’.
Usable symbols include:
It no symbol is given then :public is assumed. Unrecognized symbols raise an error.
require 'facet/module/methods' def test puts("Hello World!") end methods(:local) #=> ['test']
Synonymous with clone, this is an interesting method in that it promotes prototype-based Ruby. Now Classes aren’t the only things that respond to new.
require 'facet/object/new' "ABC".new => "ABC"
Returns the object id as a string in hexideciaml, which is how Ruby reports them with inspect.
require 'facet/object/object_hexid' "ABC".object_hexid #=> "0x402d359c"
Set setter methods and/or instance vars using a hash from another object.
require 'facet/object/set_with'
Returns a Functor that allows one to call any parent method directly.
require 'facet/object/supers'
Boolean conversion for not being nil or false.
require 'facet/object/to_b' "abc".to_bool #=> true true.to_bool #=> true false.to_bool #=> false nil.to_bool #=> false
Returns true is an object is class TrueClass, otherwise false.
require 'facet/object/bool?' true.true? #=> true false.true? #=> false nil.true? #=> false
Takes a hash and creates (singleton) attr_accessors for each key .
require 'facet/object/with_accessor' with_accessor { :x => 1, :y => 2 } @x #=> 1 @y #=> 2 self.x = 3 self.y = 4 self.x #=> 3 self.y #=> 4
Takes a hash and creates (singleton) attr_readers for each key.
require 'facet/object/with_accessor' with_reader { :x => 1, :y => 2 } @x #=> 1 @y #=> 2 self.x #=> 1 self.y #=> 2