Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/active_support/dependencies.rb,
lib/active_support/core_ext/blank.rb,
lib/active_support/core_ext/object/misc.rb,
lib/active_support/core_ext/kernel/agnostics.rb,
lib/active_support/core_ext/object/extending.rb,
lib/active_support/vendor/builder/blankslate.rb
Overview
:nodoc:
Class Method Summary collapse
- .blank_slate_method_added ⇒ Object
-
.method_added(name) ⇒ Object
Detect method additions to Object and remove them in the BlankSlate class.
Instance Method Summary collapse
-
#`(command) ⇒ Object
Makes backticks behave (somewhat more) similarly on all platforms.
-
#blank? ⇒ Boolean
“”, “ ”, nil, [], and {} are blank.
- #copy_instance_variables_from(object, exclude = []) ⇒ Object
- #extend_with_included_modules_from(object) ⇒ Object
- #extended_by ⇒ Object
- #instance_exec(*arguments, &block) ⇒ Object
- #instance_values ⇒ Object
- #load(file, *extras) ⇒ Object
- #load_without_new_constant_marking ⇒ Object
- #remove_subclasses_of(*superclasses) ⇒ Object
- #require(file, *extras) ⇒ Object
-
#returning(value) {|value| ... } ⇒ Object
A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
- #subclasses_of(*superclasses) ⇒ Object
- #to_json ⇒ Object
-
#unloadable(const_desc) ⇒ Object
Mark the given constant as unloadable.
- #with_options(options) {|ActiveSupport::OptionMerger.new(self, options)| ... } ⇒ Object
Class Method Details
.blank_slate_method_added ⇒ Object
53 |
# File 'lib/active_support/vendor/builder/blankslate.rb', line 53 alias_method :blank_slate_method_added, :method_added |
.method_added(name) ⇒ Object
Detect method additions to Object and remove them in the BlankSlate class.
57 58 59 60 61 |
# File 'lib/active_support/vendor/builder/blankslate.rb', line 57 def method_added(name) blank_slate_method_added(name) return if self != Object Builder::BlankSlate.hide(name) end |
Instance Method Details
#`(command) ⇒ Object
Makes backticks behave (somewhat more) similarly on all platforms. On win32 ‘nonexistent_command` raises Errno::ENOENT; on Unix, the spawned shell prints a message to stderr and sets $?. We emulate Unix on the former but not the latter.
6 7 8 9 10 |
# File 'lib/active_support/core_ext/kernel/agnostics.rb', line 6 def `(command) #:nodoc: super rescue Errno::ENOENT => e STDERR.puts "#$0: #{e}" end |
#blank? ⇒ Boolean
“”, “ ”, nil, [], and {} are blank
3 4 5 6 7 8 9 10 11 |
# File 'lib/active_support/core_ext/blank.rb', line 3 def blank? if respond_to?(:empty?) && respond_to?(:strip) empty? or strip.empty? elsif respond_to?(:empty?) empty? else !self end end |
#copy_instance_variables_from(object, exclude = []) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/active_support/core_ext/object/extending.rb', line 24 def copy_instance_variables_from(object, exclude = []) exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables instance_variables = object.instance_variables - exclude.map { |name| name.to_s } instance_variables.each { |name| instance_variable_set(name, object.instance_variable_get(name)) } end |
#extend_with_included_modules_from(object) ⇒ Object
31 32 33 |
# File 'lib/active_support/core_ext/object/extending.rb', line 31 def extend_with_included_modules_from(object) object.extended_by.each { |mod| extend mod } end |
#extended_by ⇒ Object
19 20 21 22 |
# File 'lib/active_support/core_ext/object/extending.rb', line 19 def extended_by ancestors = class << self; ancestors end ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] end |
#instance_exec(*arguments, &block) ⇒ Object
43 44 45 |
# File 'lib/active_support/core_ext/object/extending.rb', line 43 def instance_exec(*arguments, &block) block.bind(self)[*arguments] end |
#instance_values ⇒ Object
35 36 37 38 39 40 |
# File 'lib/active_support/core_ext/object/extending.rb', line 35 def instance_values instance_variables.inject({}) do |values, name| values[name[1..-1]] = instance_variable_get(name) values end end |
#load(file, *extras) ⇒ Object
487 488 489 490 491 492 |
# File 'lib/active_support/dependencies.rb', line 487 def load(file, *extras) Dependencies.new_constants_in(Object) { super(file, *extras) } rescue Exception => exception # errors from loading file exception.blame_file! file raise end |
#load_without_new_constant_marking ⇒ Object
485 |
# File 'lib/active_support/dependencies.rb', line 485 alias_method :load_without_new_constant_marking, :load |
#remove_subclasses_of(*superclasses) ⇒ Object
2 3 4 |
# File 'lib/active_support/core_ext/object/extending.rb', line 2 def remove_subclasses_of(*superclasses) Class.remove_class(*subclasses_of(*superclasses)) end |
#require(file, *extras) ⇒ Object
494 495 496 497 498 499 |
# File 'lib/active_support/dependencies.rb', line 494 def require(file, *extras) Dependencies.new_constants_in(Object) { super(file, *extras) } rescue Exception => exception # errors from required file exception.blame_file! file raise end |
#returning(value) {|value| ... } ⇒ Object
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']
22 23 24 25 |
# File 'lib/active_support/core_ext/object/misc.rb', line 22 def returning(value) yield(value) value end |
#subclasses_of(*superclasses) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_support/core_ext/object/extending.rb', line 6 def subclasses_of(*superclasses) subclasses = [] ObjectSpace.each_object(Class) do |k| next if # Exclude this class if (k.ancestors & superclasses).empty? || # It's not a subclass of our supers superclasses.include?(k) || # It *is* one of the supers eval("! defined?(::#{k})") || # It's not defined. eval("::#{k}").object_id != k.object_id subclasses << k end subclasses end |
#to_json ⇒ Object
31 32 33 |
# File 'lib/active_support/core_ext/object/misc.rb', line 31 def to_json ActiveSupport::JSON.encode(self) end |
#unloadable(const_desc) ⇒ Object
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.
514 515 516 |
# File 'lib/active_support/dependencies.rb', line 514 def unloadable(const_desc) Dependencies.mark_for_unload const_desc end |
#with_options(options) {|ActiveSupport::OptionMerger.new(self, options)| ... } ⇒ Object
27 28 29 |
# File 'lib/active_support/core_ext/object/misc.rb', line 27 def () yield ActiveSupport::OptionMerger.new(self, ) end |