From b794765296c3e5bbe502697158ac820b0ee5c40e Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Thu, 16 Jul 2009 02:25:48 +0200 Subject: [PATCH] AS guide: documents extensions related to instance variables --- .../source/active_support_overview.textile | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index b6168f84da..aea77c8d4e 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -316,6 +316,98 @@ end TIP: Since +with_options+ forwards calls to its receiver they can be nested. Each nesting level will merge inherited defaults in addition to their own. +h4. Instance Variables + +Active Support provides several methods to ease access to instance variables. + +h5. +instance_variable_defined?+ + +The method +instance_variable_defined?+ exists in Ruby 1.8.6 and later, and it is defined for previous versions anyway: + + +class C + def initialize + @a = 1 + end + + def m + @b = 2 + end +end + +c = C.new + +c.instance_variable_defined?("@a") # => true +c.instance_variable_defined?(:@a) # => true +c.instance_variable_defined?("a") # => NameError: `a' is not allowed as an instance variable name + +c.instance_variable_defined?("@b") # => false +c.m +c.instance_variable_defined?("@b") # => true + + +h5. +instance_variable_names+ + +Ruby 1.8 and 1.9 have a method called +instance_variables+ that returns the names of the defined instance variables. But they behave differently, in 1.8 it returns strings whereas in 1.9 it returns symbols. Active Support defines +instance_variable_names+ as a portable way to obtain them as strings: + + +class C + def initialize(x, y) + @x, @y = x, y + end +end + +C.new(0, 1).instance_variable_names # => ["@y", "@x"] + + +WARNING: The order in which the names are returned is unespecified, and it indeed depends on the version of the interpreter. + +h5. +instance_values+ + +The method +instance_values+ returns a hash that maps instance variable names without "@" to their +corresponding values. Keys are strings both in Ruby 1.8 and 1.9: + + +class C + def initialize(x, y) + @x, @y = x, y + end +end + +C.new(0, 1).instance_values # => {"x" => 0, "y" => 1} + + +h5. +copy_instance_variables_from(object, exclude = [])+ + +Copies the instance variables of +object+ into +self+. + +Instance variable names in the +exclude+ array are ignored. If +object+ +responds to +protected_instance_variables+ the ones returned are +also ignored. For example, Rails controllers implement that method. + +In both arrays strings and symbols are understood, and they have to include +the at sign. + + +class C + def initialize(x, y, z) + @x, @y, @z = x, y, z + end + + def protected_instance_variables + %w(@z) + end +end + +a = C.new(0, 1, 2) +b = C.new(3, 4, 5) + +a.copy_instance_variables_from(b, [:@y]) +# a is now: @x = 3, @y = 1, @z = 2 + + +In the example +object+ and +self+ are of the same type, but they don't need to. + h4. Silencing Warnings, Streams, and Exceptions The methods +silence_warnings+ and +enable_warnings+ change the value of +$VERBOSE+ accordingly for the duration of their block, and reset it afterwards: