Returns true if the object is nil or empty (if applicable)
[].blank? #=> true [1].blank? #=> false [nil].blank? #=> false
@return [TrueClass, FalseClass]
@api public
# File lib/extlib/blank.rb, line 12 12: def blank? 13: nil? || (respond_to?(:empty?) && empty?) 14: end
@param name
@return [Object] The constant corresponding to the name.
# File lib/extlib/object.rb, line 67 67: def full_const_get(name) 68: list = name.split("::") 69: list.shift if list.first.blank? 70: obj = self 71: list.each do |x| 72: # This is required because const_get tries to look for constants in the 73: # ancestor chain, but we only want constants that are HERE 74: obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x) 75: end 76: obj 77: end
@param name
@return [Object] The constant corresponding to the name.
# File lib/extlib/object.rb, line 83 83: def full_const_set(name, value) 84: list = name.split("::") 85: toplevel = list.first.blank? 86: list.shift if toplevel 87: last = list.pop 88: obj = list.empty? ? Object : Object.full_const_get(list.join("::")) 89: obj.const_set(last, value) if obj && !obj.const_defined?(last) 90: end
@param arrayish<#> Container to check, to see if it includes the object.
@param *more | additional args, will be flattened into arrayish |
@return [Boolean]
True if the object is included in arrayish (+ more)
@example 1.in?([1,2,3]) #=> true @example 1.in?(1,2,3) #=> true
# File lib/extlib/object.rb, line 163 163: def in?(arrayish,*more) 164: arrayish = more.unshift(arrayish) unless more.empty? 165: arrayish.include?(self) 166: end
Get the inner_html of the REXML node.
# File lib/extlib/hash.rb, line 406 406: def inner_html 407: @children.join 408: end
# File lib/extlib/object.rb, line 174 174: def instance_variable_defined?(variable) 175: instance_variables.include?(variable.to_s) 176: end
Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.
@param name
@return [nil]
# File lib/extlib/object.rb, line 98 98: def make_module(string) 99: current_module = self 100: string.split('::').each do |part| 101: current_module = if current_module.const_defined?(part) 102: current_module.const_get(part) 103: else 104: current_module.const_set(part, Module.new) 105: end 106: end 107: current_module 108: end
Extracts the singleton class, so that metaprogramming can be done on it.
@return [Class] The meta class.
@example [Setup]
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
@example
MyString.new("Hello").foo #=> "Hello"
@example
MyString.new("Hello").bar #=> NoMethodError: undefined method `bar' for "Hello":MyString
@example
MyString.foo #=> NoMethodError: undefined method `foo' for MyString:Class
@example
MyString.bar #=> MyString
@example
String.bar #=> NoMethodError: undefined method `bar' for String:Class
@example
MyString.add_meta_var(:x) MyString.x #=> HELLO
@details [Description of Examples]
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: http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
# File lib/extlib/object.rb, line 62 62: def meta_class() class << self; self end end
@param duck<Symbol, Class, Array> The thing to compare the object to.
@note
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.
@return [Boolean]
True if the object quacks like duck.
# File lib/extlib/object.rb, line 122 122: def quacks_like?(duck) 123: case duck 124: when Symbol 125: self.respond_to?(duck) 126: when Class 127: self.is_a?(duck) 128: when Array 129: duck.any? {|d| self.quacks_like?(d) } 130: else 131: false 132: end 133: end
Converts the node into a readable HTML node.
@return [String] The HTML node in text form.
# File lib/extlib/hash.rb, line 413 413: def to_html 414: attributes.merge!(:type => @type ) if @type 415: "<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>" 416: end
Convert basic XML entities into their literal values.
@param value<#> An XML fragment.
@return [#] The XML fragment after converting entities.
# File lib/extlib/hash.rb, line 389 389: def translate_xml_entities(value) 390: value.gsub(/</, "<"). 391: gsub(/>/, ">"). 392: gsub(/"/, '"'). 393: gsub(/'/, "'"). 394: gsub(/&/, "&") 395: end
If receiver is callable, calls it and returns result. If not, just returns receiver itself
@return [Object]
# File lib/extlib/object.rb, line 147 147: def try_call(*args) 148: if self.respond_to?(:call) 149: self.call(*args) 150: else 151: self 152: end 153: end
Override this in a child if it cannot be dup’ed
@return [Object]
# File lib/extlib/object.rb, line 138 138: def try_dup 139: self.dup 140: end
Override this in a child if it cannot be dup’ed
@return [Object]
# File lib/extlib/try_dup.rb, line 5 5: def try_dup 6: self.dup 7: end
Typecasts a value based upon its type. For instance, if node has # == “integer”, {{[node.typecast_value(“12”) #=> 12]}}
@param value
@details [:type options]
"integer":: converts +value+ to an integer with #to_i "boolean":: checks whether +value+, after removing spaces, is the literal "true" "datetime":: Parses +value+ using Time.parse, and returns a UTC Time "date":: Parses +value+ using Date.parse
@return [Integer, Boolean, Time, Date, Object]
The result of typecasting +value+.
@note
If +self+ does not have a "type" key, or if it's not one of the options specified above, the raw +value+ will be returned.
# File lib/extlib/hash.rb, line 378 378: def typecast_value(value) 379: return value unless @type 380: proc = self.class.typecasts[@type] 381: proc.nil? ? value : proc.call(value) 382: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.