A little help needed

T

Tom Stone

Hi,I'm new here.

I have a question:

What exactly does the '@' symbol mean when used for the scope of a
variable?
For instance: @screen=[640,400].

I haven't found anything explicitly explaining exactly what this does.

Thanks!
 
J

Jesús Gabriel y Galán

Hi,I'm new here.

I have a question:

What exactly does the '@' symbol mean when used for the scope of a
variable?
For instance: @screen=[640,400].

I haven't found anything explicitly explaining exactly what this does.

It means it's an instance variable of the object that is currently
self in that context.
For example:

class A
def initialize
@value = 3
end
end

a = A.new

The object referenced by a has an instance variable called @value with value 3.

Jesus.
 
T

Tom Stone

Oh! I see!

So it's a variable that is automtically given to each instance of that
class right?
 
J

Jonathan Nielsen

So it's a variable that is automtically given to each instance of that
class right?

Not quite... it's a variable in the instance that it is declared. So,
if you put it in the 'initialize' method as seen in that example, it
will assign that variable for that instance when it is created.

-Jonathan Nielsen
 
T

Tom Stone

Jonathan said:
Not quite... it's a variable in the instance that it is declared. So,
if you put it in the 'initialize' method as seen in that example, it
will assign that variable for that instance when it is created.

-Jonathan Nielsen

So... it's a variable that can only be used in that instance and nowhere
else?

such as:

Class Test

def initialize
@x=5
end

def testing
puts"#{@x}"
end
end

problem=Test.new

problem.testing
 
J

Jonathan Nielsen

problem=Test.new
problem.testing

Yes, it can only be used within that class. You can, however, create
an accessor for it like this:

class Test
def initialize
@x = 5
end

attr_accessor :x
end

solution = Test.new
solution.x (=>5)

solution.x = 6
solution.x (=>6)


-Jonathan Nielsen
 
T

Tom Stone

Jonathan said:
Yes, it can only be used within that class. You can, however, create
an accessor for it like this:

class Test
def initialize
@x = 5
end

attr_accessor :x
end

solution = Test.new
solution.x (=>5)

solution.x = 6
solution.x (=>6)


-Jonathan Nielsen

Yep, already knew that. Thanks for clearing this up for me. I'm creating
a game for school you see so I needed to know exacly what that meant.

(Now I can understand more of what's happening in source code) ;)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

puts"#{@x}"
You can also just say @x, here, because puts will convert the objects it
receives into strings by calling the to_s method on them
puts "#{@x}"
puts @x.to_s
puts @x

In this case will all be the same, there is no need to interpolate strings
here.

At least in 1.9.1 this is true. I tried it in 1.8 and the last one outputted
"nil" which is odd, and the only way I can think of that would explain it is
that puts checks to see if the variable is nil before calling to_s, and
explicitly outputs "nil" if it is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,413
Latest member
ReeceDorri

Latest Threads

Top