[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

basic mistake?

Nia

11/16/2006 5:15:00 AM

--------------------------------------------------------
class BookStore
@bookstore=Array.new

def initialize(*books)
i=0
books.each do |x|
#@bookstore.push(x)
puts "(#{i})" + x.inspect
i+=1
end
end
end

class Book
def initialize(name,cost)
@book_name=name
@book_cost=cost
end

def detail
puts "Book: #{@book_name} ($#{@book_cost})"
end
end

b1=Book.new('Harry Potter','9.95')
b2=Book.new('The Raven','8.65')
store = BookStore.new(b1,b2)
--------------------------------------------------------------
this bit of code seems to work fine, but when i try to assign an object to my bookstore
array, i'm given an error(i commented it out since it wasn't working). wierd part is that this
seem to work fine outside of a class. am i missing something?, like a declaration? this isn't
the first time i've encounter this problem. please help me solve it.
6 Answers

Hal E. Fulton

11/16/2006 5:21:00 AM

0

Nia wrote:
> --------------------------------------------------------
> class BookStore
> @bookstore=Array.new
>
> def initialize(*books)
> i=0
> books.each do |x|
> #@bookstore.push(x)
> puts "(#{i})" + x.inspect
> i+=1
> end
> end
> end
>
> class Book
> def initialize(name,cost)
> @book_name=name
> @book_cost=cost
> end
>
> def detail
> puts "Book: #{@book_name} ($#{@book_cost})"
> end
> end
>
> b1=Book.new('Harry Potter','9.95')
> b2=Book.new('The Raven','8.65')
> store = BookStore.new(b1,b2)
> --------------------------------------------------------------
> this bit of code seems to work fine, but when i try to assign an object to my bookstore
> array, i'm given an error(i commented it out since it wasn't working). wierd part is that this
> seem to work fine outside of a class. am i missing something?, like a declaration? this 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.

Hal


Robert Spielmann

11/16/2006 1:19:00 PM

0

Hal Fulton schrieb:
> Nia wrote:
>> --------------------------------------------------------
>> class BookStore
>> @bookstore=Array.new
>> def initialize(*books)
>> i=0
>> books.each do |x|
>> #@bookstore.push(x)
>> puts "(#{i})" + x.inspect
>> i+=1
>> end
>> end
>> end
>>
>> class Book
>> def initialize(name,cost)
>> @book_name=name
>> @book_cost=cost
>> end
>> def detail
>> puts "Book: #{@book_name} ($#{@book_cost})"
>> end
>> end
>>
>> b1=Book.new('Harry Potter','9.95')
>> b2=Book.new('The Raven','8.65')
>> store = BookStore.new(b1,b2)
>> --------------------------------------------------------------
>> this bit of code seems to work fine, but when i try to assign an
>> object to my bookstore array, i'm given an error(i commented it out
>> since it wasn't working). wierd part is that this seem to work fine
>> outside of a class. am i missing something?, like a declaration? this
>> 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


Chris Hulan

11/16/2006 2:07:00 PM

0

Robert Spielmann wrote:
> Hal Fulton schrieb:
> > Nia wrote:
> >> --------------------------------------------------------
> >> class BookStore
> >> @bookstore=Array.new
> >> def initialize(*books)
> >> i=0
> >> books.each do |x|
> >> #@bookstore.push(x)
> >> puts "(#{i})" + x.inspect
> >> i+=1
> >> end
> >> end
> >> end
....

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.

Cheers
Chris

Nia

11/16/2006 4:13:00 PM

0

> 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.
>
> Cheers
> Chris
>

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,

dblack

11/16/2006 4:25:00 PM

0

Jacob Fugal

11/16/2006 4:54:00 PM

0

On 11/16/06, Nia <badluck105@yahoo.com> wrote:
> 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 Fugal