class Object

# begin_documentation Object will be changed just by including this file or requiring rubyment, which is not the best approach, but kept to respect the open/closed principle, but new functions should NOT be added here.

only a bunch of methods, is, however, added: . negate_me . to_nil . nne

# end_documentation

Public Instance Methods

as_container(components_only=nil, method_name=:map) click to toggle source

Turns any Ruby object into a composite.

Examples:

1.as_container(:components_only).entries # => []

1.as_container.entries # => [1]

“hello”.as_container.entries # => [“hello”]

[1, 2, 3].as_container().entries # => [1, 2, 3]

[1, 2, 3].as_container(:components_only).entries # => [1, 2, 3]

# Take the first element of the operation on the object a: # a.as_container.entries - a.as_container(:only_components).entries # to find what's the non composite component of a:

a = [ 1, 2, 3 ] ; a.as_container.entries - a.as_container(:only_components).entries # => []

a = “str” ; a.as_container.entries - a.as_container(:only_components).entries # => [“str”]

# File lib/rubyment.rb, line 126
def as_container components_only=nil, method_name=:map
  self.respond_to?(method_name) && (
    self.send method_name
  ) || (!components_only) && (
    [self].send method_name
  ) || (
    [].send method_name
  )
end
negate_me(condition=true) click to toggle source

returns +!self+, unless unless_condition is true; in such case returns self. e.g: +false.negate_me true+ returns false. (by default, if no params are given, just # negates self) experiment [1, 2, nil, 3, nil, 4].select &:negate_me to find the nil elements in an array

# File lib/rubyment.rb, line 86
def negate_me condition=true
  (condition) && (
    !self
  ) || (!condition) && (
    self
  )
end
nne(default=nil) click to toggle source

returns self if self is not considered to be the neutral element of its class. an object is considered to the neutral element if it answers true to any of a global asserting_methods list. this list is by now the following:

empty?, :zero?, :false?

but it will grow as more classes are known to have different asserting_methods. it returns nil when the element is considered the neutral element.

data from other languagues, like most shell input, can't be nil so sometimes “” has to be interpreted as nil. so, one example usage of the neutral element concept.

# File lib/rubyment.rb, line 54
def nne default=nil
  asserting_methods = [
    :empty?,
    :zero?,
    :false?,
  ]
  responds_false_to = asserting_methods.map { |am|
    (self.respond_to? am) && (
      self.method(am).call.negate_me
    ) || nil
  }
  responds_false_to.any? && self || default
end
to_nil() click to toggle source

returns nil out of an object. usage examples: “this_parameter_is_nil”.to_nil

# File lib/rubyment.rb, line 72
def to_nil
  nil
end