1

For example, in the code snippets in the Fog gem, e.g.

require 'rubygems'
require 'fog'

# create a connection to the service
dns = Fog::DNS.new({
  :provider     => 'Zerigo',
  :zerigo_email => ZERIGO_EMAIL,
  :zerigo_token => ZERIGO_TOKEN
})


zone = @dns.zones.create(
  :domain => 'example.com',
  :email  => '[email protected]'
)

Why dns is not instance variable at the first place, but then become an instance variable in the next paragraph? Why zone is not instance variable?

1 Answer 1

1

This typically happens when using an attribute reader/writer, as attr_reader, attr_writer, attr_accessor. These macros create an instance variable and expose for it a 'getter' method, a 'setter' method, or both respectively. Inside the instance methods, the instance variable will always be accessible.

Regarding the specific code used in the example, it appears that dns is a standard local variable. If it were actually an attribute accessor, it should have been written as:

self.dns = Fog::DNS.new({...

because the attribute accessor is actually a method, not a local variable. So you would be correct in stating that it cannot simply 'become' an instance variable after being declared as a local variable, and the sample code as it currently stands is an inaccurate representation of real world use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.