what is a different? @name and name on ruby? sometime I confuse these…
OnRails [email protected] wrote:
what is a different? @name and name on ruby? sometime I confuse these…
On Oct 19, 2007, at 10:31 AM, OnRails wrote:
what is a different? @name and name on ruby? sometime I confuse
these…
@name is an “instance variable”
name is a local variable.
In a class, like the one below, @birthdate is used in two places: in
getBirthdate and in ageNow. In order to use it inside more than one
method it needs to be an instance variable (a var with an @).
However, in ageNow, we create a local var called currentDate (it has
no @). It is used in the formula to calculate age. We also need the
current date in dateToday, but we cannot reuse the ‘currentDate’ var
because it is local to only the ageNow method. So, in dateToday we
create another local var dateOfToday.
class Person
def initialize
@birthdate = ‘1950-06-15’
end
def getBirthday
return @birthdate
end
def ageNow
currentDate = Date.now
ageInYears = Integer.new
currentDate - @birthdate = ageInYears # fake formula
return ageInYears
end
def dateToday
dateOfToday = Date.now
return dateOfToday
end
end
– gw
On Oct 19, 11:11 am, Greg W. [email protected] wrote:
getBirthdate and in ageNow. In order to use it inside more than one
@birthdate = ‘1950-06-15’
currentDate - @birthdate = ageInYears # fake formula– gw
Thank you, gw!. I got it!!
No, that is an instance variable of that class so it will only be
available to a particular instance (not entirely true because of the
rather rude instance_variable_get method). You can write an accessor
method to allow access to it. That is the whole point of encapsulation,
to provide an interface to the implementation and shield the user of
your class from the gory details. I would suggest reading up on basic
object oriented principals (Ruby specific or otherwise) as since
everything in Ruby is an object, a good understanding of these
principles will be a major help. You should look at the book
“Programming Ruby” aka the “Pickaxe” book. It is very well written and
has all the information you could want on this subject.
-Bill
elle wrote:
name is a local variable.
class Person
currentDate = Date.now
end
–
Sincerely,
William P.
Does that mean that @birthdate will be accessible to other classes
outside this one?
Not different classes: the “scope” of a variable determines how
visible it is to other parts of the application. A local variable
(name, with no @ or @@) is visible/accessible/usable only within the
method in which it is defined. An instance variable, one defined with
the @ sign, like @name is visible anywhere within the class, or a
subclass … but not, directly to any other class.
Now this gets a little confusing in Rails because Rails does some
special magic for you. In specific, when you define a model, for
example Employee, and that model is tied to a database with a table
called employees, all of the columns of that database table, like
“name”, “salary”, “start_date” and so on become magically defined as
instance variables of the model. You won’t see anything in the model
that defines @name as anything, but if the employee table has a column
called name, you have a free instance variable which you can reference
as @name.
Cool, eh?
Tom
Does that mean that @birthdate will be accessible to other classes
outside this one?
Elle
getBirthdate and in ageNow. In order to use it inside more than one
method it needs to be an instance variable (a var with an @)…
On Oct 19, 2007, at 5:33 PM, elle wrote:
Does that mean that @birthdate will be accessible to other classes
outside this one?
In plain Ruby, no. To make it accessible to other classes, you’d have
to use
attr_reader birthdate # read only
attribute birthdate # read and write (be careful)
or write an explicit method like the getBirthday in the previous
message’s example code.
However, as tharrison wrote, Rails takes some liberties with this in
some of its innards. If you write your own class though (i.e. not a
Rails controller or model), then you’ll be following the rules of
plain Ruby.
– gw
Thank you guys for explaining that. It’s been very helpful and by the
way I have “Programming Ruby” on order.
Just one more question: views can see the instance variable, is that
right? and if so, how do they know which instance variable they can
see?
Elle
On 20 Oct 2007, at 06:29, elle wrote:
Thank you guys for explaining that. It’s been very helpful and by the
way I have “Programming Ruby” on order.Just one more question: views can see the instance variable, is that
right? and if so, how do they know which instance variable they can
see?
ActionController magically copies its instance variables into the
object representing the views. There is a method on
ActionController#Base defining those ivars not to copy accross.
Fred