I’m convinced I’ve had this working before!
I have the following class. The title string is created within the class the fname and lname strings are passed in as parameters. However, I can’t seem to ever get the @title object to return anything other than nil.
What am I doing wrong here?
class Person
attr_accessor :fname, :lname, :title
def initialize(fname, lname)
@fname = fname
@lname = lname
@title = title
end
def string1
@lname + ", " + @fname
end
@title = "Director"
def string2
@title
end
end
p = Person.new("Yukihiro", "Matsumoto")
p p.string1
p p.string2
The following code:
gives the following output when run with Ruby 1.9.3:
I’ve assumed that you want to keep the title the same for all the objects you create. If not, then iamnotmaynard‘s answer is what you’re after.