Class: Object

Inherits:
BasicObject
Defined in:
lib/nrser/labs/i8/surjection.rb,
lib/nrser/core_ext/object.rb,
lib/nrser/core_ext/object/lazy_var.rb

Overview

HACK

Instance Method Summary collapse

Instance Method Details

#as_arrayObject

Call NRSER.as_array on ‘self`.



42
43
44
# File 'lib/nrser/core_ext/object.rb', line 42

def as_array
  NRSER.as_array self
end

#as_hash(key = nil) ⇒ Object

Calls NRSER.as_hash on ‘self` with the provided `key`.



36
37
38
# File 'lib/nrser/core_ext/object.rb', line 36

def as_hash key = nil
  NRSER.as_hash self, key
end

#falsy?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/nrser/core_ext/object.rb', line 30

def falsy?
  NRSER.falsy? self
end

#if(proc_able, &block) ⇒ Object



15
16
17
# File 'lib/nrser/labs/i8/surjection.rb', line 15

def if proc_able, &block
  block.call( self ) if proc_able.to_proc.call( self )
end

#is?(other) ⇒ Boolean

Just an alias for ‘#equal?` that is easier for to remember.

Parameters:

  • other (*)

    Something else.

Returns:

  • (Boolean)

    ‘true` if `self` and `other` are the same object.



18
19
20
# File 'lib/nrser/core_ext/object.rb', line 18

def is? other
  equal? other
end

#lazy_var(name, &block) ⇒ VALUE

If the instance variable ‘name` is not defined, sets it to the result of `&block`. Always returns the instance variable’s value.

Useful for lazy values that can be ‘nil` or `false`, since `||=` will always re-evaluate in their cases.

Parameters:

  • name (Symbol)

    The name of the instance variable. Needs to have that ‘@` on the front, like `:@x`.

  • block (Proc<() => VALUE>)

    The block to call to get the value.

Returns:

  • (VALUE)

    The value of the instance variable.



23
24
25
26
27
28
29
# File 'lib/nrser/core_ext/object/lazy_var.rb', line 23

def lazy_var name, &block
  unless instance_variable_defined? name
    instance_variable_set name, block.call
  end
  
  instance_variable_get name
end

#thru {|_self| ... } ⇒ Object

Yield ‘self`. Analogous to #tap but returns the result of the invoked block.

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



5
6
7
# File 'lib/nrser/core_ext/object.rb', line 5

def thru
  yield self
end

#truthy?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/nrser/core_ext/object.rb', line 24

def truthy?
  NRSER.truthy? self
end

#unless(proc_able, &block) ⇒ Object



19
20
21
# File 'lib/nrser/labs/i8/surjection.rb', line 19

def unless proc_able, &block
  block.call( self ) unless proc_able.to_proc.call( self )
end