It is a different way of thinking:
When you let your method access a global (or external) object, you have a dependency to that object and rely on a) that it exists and b) that it has a valid state, a correct value. Someone else (or even some other part of your code) could have changed it, and then your method fails.
Instead of letting your method fetch external data, the other way round is you pass to your method all the data it will need. Advantage is that all dependencies must be available before calling your method. By keeping the data within your object, you have it under your control.
The common way to Initialize attributes in the constructor:
def initialize(attribute1, attribute2)
@attribute1 = attribute1
@attribute2 = attribute2
attr_reader :attribute1, :attribute2
# Reading an attribute:
object1 = SomeObject.new(45, 90)
# Somewhere else someone wants to red the attribute…
object1.attribute1 # ==> 45
# If there is only a getter (attr_reader) defined, nobody else can change the attribute:
object1.attribute1 = 0 # ==> NoMethodError: undefined method `attribute1='
If it should be (like your global) shared for all instances of the class, use a class variable instead, as Steve wrote:
(In case you would have several instances; that means if one instance changes it, other instances will also work with the changed value.)
@@defangle = 0
# Reading the class variable:
SomeObject.defangle # ==> 0
This works like a global, but it doesn’t create a new
$ variable at top level, but it is grouped within your name space.