Same as above, except in Object.
send | -> | send! |
Anticipating Ruby 1.9 neutering send | ||
load | -> | load_without_new_constant_marking |
method_added | -> | blank_slate_method_added |
# File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 90 90: def find_hidden_method(name) 91: nil 92: end
Lookup missing generators using const_missing. This allows any generator to reference another without having to know its location: RubyGems, ~/.rails/generators, and RAILS_ROOT/generators.
# File vendor/rails/railties/lib/rails_generator/lookup.rb, line 8 8: def lookup_missing_generator(class_id) 9: if md = /(.+)Generator$/.match(class_id.to_s) 10: name = md.captures.first.demodulize.underscore 11: Rails::Generator::Base.lookup(name).klass 12: else 13: const_missing_before_generators(class_id) 14: end 15: end
Detect method additions to Object and remove them in the BlankSlate class.
# File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 83 83: def method_added(name) 84: result = blank_slate_method_added(name) 85: return result if self != Object 86: BlankSlate.hide(name) 87: result 88: end
A duck-type assistant method. For example, ActiveSupport extends Date to define an acts_like_date? method, and extends Time to define acts_like_time?. As a result, we can do "x.acts_like?(:time)" and "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that we want to act like Time simply need to define an acts_like_time? method.
# File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 56 56: def acts_like?(duck) 57: respond_to? "acts_like_#{duck}?" 58: end
An object is blank if it‘s nil, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.
This simplifies
if !address.nil? && !address.empty?
to
if !address.blank?
# File vendor/rails/activesupport/lib/active_support/core_ext/blank.rb, line 9 9: def blank? 10: respond_to?(:empty?) ? empty? : !self 11: end
Can you safely .dup this object? False for nil, false, true, symbols, and numbers; true otherwise.
# File vendor/rails/activesupport/lib/active_support/core_ext/duplicable.rb, line 4 4: def duplicable? 5: true 6: end
Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.
From Mauricio‘s eigenclass.org/hiki/bounded+space+instance_exec
# File vendor/rails/activesupport/lib/active_support/core_ext/object/extending.rb, line 41 41: def instance_exec(*args, &block) 42: begin 43: old_critical, Thread.critical = Thread.critical, true 44: n = 0 45: n += 1 while respond_to?(method_name = "__instance_exec#{n}") 46: InstanceExecMethods.module_eval { define_method(method_name, &block) } 47: ensure 48: Thread.critical = old_critical 49: end 50: 51: begin 52: send(method_name, *args) 53: ensure 54: InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil 55: end 56: end
# File vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 4 4: def instance_variable_defined?(variable) 5: instance_variables.include?(variable.to_s) 6: end
A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
def foo returning values = [] do values << 'bar' values << 'baz' end end foo # => ['bar', 'baz'] def foo returning [] do |values| values << 'bar' values << 'baz' end end foo # => ['bar', 'baz']
# File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 27 27: def returning(value) 28: yield(value) 29: value 30: end
Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
# File vendor/rails/activesupport/lib/active_support/json/encoders/object.rb, line 3 3: def to_json(options = {}) 4: ActiveSupport::JSON.encode(instance_values, options) 5: end
Alias of to_s.
# File vendor/rails/activesupport/lib/active_support/core_ext/object/conversions.rb, line 3 3: def to_param 4: to_s 5: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 7 7: def to_param 8: to_s 9: end
Converts an object into a string suitable for use as a URL query string, using the given key as the param name.
Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
# File vendor/rails/activesupport/lib/active_support/core_ext/object/conversions.rb, line 11 11: def to_query(key) 12: "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}" 13: end
Mark the given constant as unloadable. Unloadable constants are removed each time dependencies are cleared.
Note that marking a constant for unloading need only be done once. Setup or init scripts may list each unloadable constant that may need unloading; each constant will be removed for every subsequent clear, as opposed to for the first clear.
The provided constant descriptor may be a (non-anonymous) module or class, or a qualified constant name as a string or symbol.
Returns true if the constant was not previously marked for unloading, false otherwise.
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 515 515: def unloadable(const_desc) 516: Dependencies.mark_for_unload const_desc 517: end
An elegant way to refactor out common options
with_options :order => 'created_at', :class_name => 'Comment' do |post| post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all post.has_many :unapproved_comments, :conditions => ['approved = ?', false] post.has_many :all_comments end
Can also be used with an explicit receiver:
map.with_options :controller => "people" do |people| people.connect "/people", :action => "index" people.connect "/people/:id", :action => "show" end
# File vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb, line 47 47: def with_options(options) 48: yield ActiveSupport::OptionMerger.new(self, options) 49: end