Class: Object

Inherits:
BasicObject
Defined in:
lib/merb-core/core_ext/object.rb,
lib/merb-core/test/test_ext/object.rb

Instance Method Summary collapse

Instance Method Details

#assigns(attr) ⇒ Object

Parameters

attr<~to_s>

The name of the instance variable to get.

Returns

Object

The instance variable @attr for this object.

Examples

# In a spec
@my_obj.assigns(:my_value).should == @my_value


11
12
13
# File 'lib/merb-core/test/test_ext/object.rb', line 11

def assigns(attr)
  self.instance_variable_get("@#{attr}")
end

#blank?Boolean

Returns

Boolean

True if the empty? is true or if the object responds to strip (e.g. a String) and strip.empty? is true, or if !self is true.

Examples

[].blank?         #=>  true
[1].blank?        #=>  false
[nil].blank?      #=>  false
nil.blank?        #=>  true
true.blank?       #=>  false
false.blank?      #=>  true
"".blank?         #=>  true
"     ".blank?    #=>  true
" hey ho ".blank? #=>  false

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/merb-core/core_ext/object.rb', line 71

def blank?
  if respond_to?(:empty?) && respond_to?(:strip)
    empty? or strip.empty?
  elsif respond_to?(:empty?)
    empty?
  else
    !self
  end
end

#full_const_get(name) ⇒ Object

Parameters

name<String>

The name of the constant to get, e.g. “Merb::Router”.

Returns

Object

The constant corresponding to the name.



86
87
88
89
90
91
# File 'lib/merb-core/core_ext/object.rb', line 86

def full_const_get(name)
  list = name.split("::")
  obj = Object
  list.each {|x| obj = obj.const_get(x) }
  obj
end

#make_module(str) ⇒ Object

Makes a module from a string (e.g. Foo::Bar::Baz)

Parameters

name<String>

The name of the full module name to make

Returns

nil



100
101
102
103
104
105
106
107
108
# File 'lib/merb-core/core_ext/object.rb', line 100

def make_module(str)
  mod = str.split("::")
  start = mod.map {|x| "module #{x}"}.join("; ")
  ender = (["end"] * mod.size).join("; ")
  self.class_eval <<-HERE
    #{start}
    #{ender}
  HERE
end

#meta_classObject

Extracts the singleton class, so that metaprogramming can be done on it.

Returns

Class

The meta class.

Examples

class MyString < String; end

MyString.instance_eval do
  define_method :foo do
    puts self
  end
end

MyString.meta_class.instance_eval do
  define_method :bar do
    puts self
  end
end

def String.add_meta_var(var)
  self.meta_class.instance_eval do
    define_method var do
      puts "HELLO"
    end
  end
end

MyString.new("Hello").foo #=> "Hello"
MyString.new("Hello").bar
  #=> NoMethodError: undefined method `bar' for "Hello":MyString
MyString.foo
  #=> NoMethodError: undefined method `foo' for MyString:Class
MyString.bar
  #=> MyString
String.bar
  #=> NoMethodError: undefined method `bar' for String:Class

MyString.add_meta_var(:x)
MyString.x #=> HELLO

As you can see, using #meta_class allows you to execute code (and here, define a method) on the metaclass itself. It also allows you to define class methods that can be run on subclasses, and then be able to execute code on the metaclass of the subclass (here MyString).

In this case, we were able to define a class method (add_meta_var) on String that was executable by the MyString subclass. It was then able to define a method on the subclass by adding it to the MyString metaclass.

For more information, you can check out _why’s excellent article at: whytheluckystiff.net/articles/seeingMetaclassesClearly.html



54
# File 'lib/merb-core/core_ext/object.rb', line 54

def meta_class() class << self; self end end

#quacks_like?(duck) ⇒ Boolean

Parameters

duck<Symbol, Class, Array>

The thing to compare the object to.

Notes

The behavior of the method depends on the type of duck as follows:

Symbol

Check whether the object respond_to?(duck).

Class

Check whether the object is_a?(duck).

Array

Check whether the object quacks_like? at least one of the options in the array.

Returns

Boolean

True if the object quacks like duck.

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/merb-core/core_ext/object.rb', line 123

def quacks_like?(duck)
  case duck
  when Symbol
    self.respond_to?(duck)
  when Class
    self.is_a?(duck)
  when Array
    duck.any? {|d| self.quacks_like?(d) }
  else
    false
  end
end