Class: Object
- Inherits:
- BasicObject
- Includes:
- URI::Kernel
- Defined in:
- lib/standard/facets/uri.rb,
lib/core/facets/object/dup.rb,
lib/standard/facets/against.rb,
lib/standard/facets/nullclass.rb,
lib/core/facets/object/object_state.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#against(*msg, &blk) ⇒ Object
Objectified message or block application.
- #clone? ⇒ Boolean
-
#dup? ⇒ Boolean
Can you safely call #dup on this object?.
- #null? ⇒ Boolean
-
#object_state(data = nil) ⇒ Object
Get or set state of object.
Methods included from URI::Kernel
Instance Method Details
#against(*msg, &blk) ⇒ Object
Objectified message or block application. Only a message or a block can be given, not both.
msg - method and arguments [Array] blk - procedure block [Proc]
Examples
a = [1,2,3,4,5]
c = a.against(:>, 2)
c.select #=> [3,4,5]
a = [1,2,3,4,5]
c = a.against(:>)
c.select(2) #=> [3,4,5]
Returns [Functor]
TODO: Better name for this method?
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/standard/facets/against.rb', line 25 def against(*msg, &blk) raise ArgumentError, "too many arguments" if blk && !msg.empty? this = self blk = ::Proc.new{ |x,*a| x.__send__(*msg, *a) } unless blk #if blk Functor.new do |m, *a, &b| if b b2 = ::Proc.new{ |*x| blk.call(*b.call(*x), *a) } else b2 = blk end this.__send__(m, &b2) end #else # Functor.new do |m, *a, &b| # if b # b2 = ::Proc.new{ |*x| b.call(*x).__send__(*msg, *a) } # else # b2 = ::Proc.new{ |x| x.__send__(*msg, *a) } # end # this.__send__(m, &b2) # end #end end |
#clone? ⇒ Boolean
7 |
# File 'lib/core/facets/object/dup.rb', line 7 def clone? ; true ; end |
#dup? ⇒ Boolean
Can you safely call #dup on this object?
Returns false for nil, false, true, symbols, and numbers; true otherwise.
6 |
# File 'lib/core/facets/object/dup.rb', line 6 def dup? ; true ; end |
#null? ⇒ Boolean
33 34 35 |
# File 'lib/standard/facets/nullclass.rb', line 33 def null? false end |
#object_state(data = nil) ⇒ Object
Get or set state of object. You can think of #object_state as an in-code form of marshalling.
class StateExample
attr_reader :a, :b
def initialize(a,b)
@a, @b = a, b
end
end
obj = StateExample.new(1,2)
obj.a #=> 1
obj.b #=> 2
obj.object_state #=> {:a=>1, :b=>2}
obj.object_state(:a=>3, :b=>4)
obj.a #=> 3
obj.b #=> 4
For most object’s this is essentially the same as instance.to_h. But for data structures like Array and Hash it returns a snapshot of their contents, not the state of their instance variables. – TODO: Should this be in module Kernel ? ++
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/core/facets/object/object_state.rb', line 28 def object_state(data=nil) if data instance_variables.each do |iv| name = iv.to_s.sub(/^[@]/, '').to_sym instance_variable_set(iv, data[name]) end else data = {} instance_variables.each do |iv| name = iv.to_s.sub(/^[@]/, '').to_sym data[name] = instance_variable_get(iv) end data end end |