Class: Object

Inherits:
BasicObject
Defined in:
lib/nuggets/object/blank.rb,
lib/nuggets/object/msend.rb,
lib/nuggets/object/boolean.rb,
lib/nuggets/object/singleton_class.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright © 2007-2008 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@uni-koeln.de>                                    #
                                                                        #

ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. #

#

You should have received a copy of the GNU General Public License along # with ruby-nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Instance Method Summary collapse

Instance Method Details

#blank?(*modifiers) ⇒ Boolean

call-seq:

object.blank? => true or false

Basically a short-cut to object.nil? || object.empty?.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nuggets/object/blank.rb', line 34

def blank?(*modifiers)
  if block_given?
    return true if yield(dup).blank?
  end

  if modifiers.empty?
    respond_to?(:empty?) ? empty? : !self
  else
    return true if blank?

    modifiers.each { |modifier|
      if respond_to?(modifier)
        if modifier.to_s =~ /\?\z/
          return true if send(modifier)
        else
          return true if send(modifier).blank?
        end
      end
    }

    false
  end
end

#boolean?Boolean

call-seq:

object.boolean? => true or false

Returns:

  • (Boolean)


34
35
36
# File 'lib/nuggets/object/boolean.rb', line 34

def boolean?
  is_a?(TrueClass) || is_a?(FalseClass)
end

#msend(*messages) ⇒ Object

call-seq:

object.msend(*messages) => anArray

Sends object multiple messages and returns an array of the individual return values.



35
36
37
38
# File 'lib/nuggets/object/msend.rb', line 35

def msend(*messages)
  hash = messages.last.is_a?(Hash) ? messages.pop : {}
  (messages + hash.to_a).map { |msg| send *msg.is_a?(Array) ? msg : [msg] }
end

#negateObject Also known as: false?

call-seq:

object.negate => true or false


42
43
44
# File 'lib/nuggets/object/boolean.rb', line 42

def negate
  !self
end

#singleton_classObject Also known as: virtual_class, ghost_class, eigenclass, metaclass, uniclass

call-seq:

object.singleton_class => aClass

Returns the singleton (or virtual/eigen/meta) class associated with object.



34
35
36
# File 'lib/nuggets/object/singleton_class.rb', line 34

def singleton_class
  class << self; self; end
end

#singleton_class?Boolean Also known as: virtual_class?, ghost_class?, eigenclass?, metaclass?, uniclass?

call-seq:

object.singleton_class? => true or false

Returns true if object is a singleton_class (i.e., has a singleton_object), false otherwise.

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/nuggets/object/singleton_class.rb', line 79

def singleton_class?
  singleton_object
  true
rescue TypeError
  false
end

#singleton_objectObject Also known as: virtual_object, ghost_object, eigenobject, metaobject, uniobject, singleton_instance

call-seq:

object.singleton_object => anObject

Returns the object of which object is the singleton_class. Raises a TypeError if object is not a singleton class.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nuggets/object/singleton_class.rb', line 49

def singleton_object
  [true, false, nil].each { |obj|
    return obj if self.equal?(obj.singleton_class)
  }

  # raises TypeError if neither class nor module
  ObjectSpace.each_object(self) { |obj|
    return obj if self.equal?(obj.singleton_class)
  }

  # if we got here, it can't be a singleton class
  # or its singleton object doesn't exist anymore
  raise TypeError
rescue TypeError
  raise TypeError, 'not a singleton class'
end

#to_boolObject Also known as: true?

call-seq:

object.to_bool => true or false


52
53
54
# File 'lib/nuggets/object/boolean.rb', line 52

def to_bool
  !!self
end

#void?Boolean Also known as: vain?

call-seq:

object.void? => true or false

Adds white-space strings, 0 and arrays of nil objects to the list of blank objects.

Returns:

  • (Boolean)


63
64
65
# File 'lib/nuggets/object/blank.rb', line 63

def void?
  blank?(:zero?, :strip, :compact)
end