def detail
seem to work fine outside of a class. am i missing something?, like a declaration? this isn’t
You can also consolidate part of the initialize method like so:
def initialize(*books)
@bookstore=Array.new
books.each_with_index do |x,i| @bookstore.push(x)
puts “(#{i})” + x.inspect
end
end
end
isn’t the first time i’ve encounter this problem. please help me
solve it.
At least two mistakes here… move the @bookstore line
down into the initialize method. And take off the #
from that line – that makes it a comment.
Which is what (s)he intentionally did (“i commented it out since it
wasn’t working”)
Hal
Robert
Hal is correct, moving it into initialize solves the problem.
It appears that declaring the instance variable at the class
level makes it an instance variable of the class, rather than
of the object of the class being created when initialize is called.
In short, declare your instance attributes inside initialize.
hehe, i knew it was some basic mistake. thank you all very much. so
is it wrong to declare a variable outside a method? cause i usually
do that a lot,
It’s wrong to initialize a variable in one scope and try to use it in
another Actually there are (at least) two things to keep in mind:
Local variables are visible in their local scope. So here:
class C
x = 1
def meth
x = 2
end
end
the two x’s are totally unrelated.
Instance variables are scoped to, or associated with, “self”. In
other words, an instance variable (@var) always belongs to whatever
object is “self” at that point. Here:
class C @x = 1 # (a)
def meth @x = 2 # (b)
end
end
you’re dealing with two selfs (selves?). At (a), self is the class
object itself, C. At (b), self will be whatever instance of C calls
the methods “meth”. So, since there are two selfs, the two @x’s are
totally unrelated to each other.
so is it wrong to declare a
variable outside a method? cause i usually do that a lot,
This misunderstanding probably stems from the following type of code:
NOTE: no enclosing class
@a = “foo” # (a)
def bar
puts @a # (b)
end
bar # => prints “foo”
Se here, the @a seems to be able to stretch across the method
definition. Why?
The reason is because all “procedural” (top level scope) code is
invoked in the context of the “main” object. At (a), self is “main”,
so @a belongs to the “main” object. When you define methods in context
of the “main” object those methods are actually created as instance
methods on Object, which makes them available everywhere (since every
object in Ruby is_a? Object). So the @a at (b) refers to the @a of
whoever calls bar. At (c), “main” is calling bar, so its value of @a
(set at (a)) is used.
Let’s try calling bar from the context of an object that doesn’t have @a
set:
@a = “foo” # belongs to “main”
def bar
puts(@a || “@a is empty!”)
end
bar # => prints “foo”
module A
# “self” here is the module itself
bar # => prints “@a is empty!”
end
So, the moral? The above construct where you have @a defined at the
top level and then (tried) to use in a top level method only works by
accident. While it works in the common case where used, it’s not
correct and can lead to errors when the top level method is called in
a context other than the top level. If you really need that sort
of behavior, use globals ($a). That’s what they’re for. But, of
course, if you find yourself needing globals you probably want to
rethink your approach.
Hope that made sense…
Jacob F.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.