attribute reader problem

A

aidy

Hi,

I have assigned the current date to an instance variable and attempted
to use an attribute reader.

This is the code:

class Dates
attr_reader :current_date

def setCurrentDate
@current_date = Time.now.localtime.strftime("%d/%m/%Y")
end

end

I instantiate this and try to read the current date

d = Dates.new.current_date
p d

nil is returned (twice)

Although, if I do this

p Dates.new.setCurrentDate

I get two current dates, but I am not breaking Object rules?

I have also tried a traditional getter method with the same result.

Thank You

Aidy
 
R

Robert Klemme

aidy said:
Hi,

I have assigned the current date to an instance variable and attempted
to use an attribute reader.

This is the code:

class Dates
attr_reader :current_date

def setCurrentDate
@current_date = Time.now.localtime.strftime("%d/%m/%Y")
end

end

I instantiate this and try to read the current date

d = Dates.new.current_date
p d

nil is returned (twice)

Well, of course since you do not set the instance variable. The second
nil is from method "p".
Although, if I do this

p Dates.new.setCurrentDate

I get two current dates, but I am not breaking Object rules?

I have also tried a traditional getter method with the same result.

What do you want? Do you want to have current_date set on object
creation? In that case you must invoke set_current_date from method
initialize(). Note also, that in Ruby it's convention to
use_lowe_case_names instead of camelCase.

Kind regards

robert
 
A

aidy

Hi Robert,
Thanks for getting back
you do not set the instance variable.
I am not so sure what I am to set the instance variable to. I was under
the impression that I assigned the instance var here

@current_date = Time.now.localtime.strftime("%d/%m/%Y")

And this
attr_reader :current_date

was just a way of doing this

def get_current_date
current_date = @current_date
end
The second nil is from method "p". thanks

Do you want to have current_date set on object creation?

No, i don't think so, because I want other methods in the class (for
example:
#current_date_plus_year or something)
Note also, that in Ruby it's convention to use_lowe_case_names instead of >camelCase.
Note taken

Aidy
 
C

ChrisH

aidy said:
I am not so sure what I am to set the instance variable to. I was under
the impression that I assigned the instance var here

@current_date = Time.now.localtime.strftime("%d/%m/%Y")

This assignment only occurs when you call the set_current_date method.
And this
attr_reader :current_date

was just a way of doing this

def get_current_date
current_date = @current_date
end

attr_reader: current_date creates the instance var @current_date and a
getter method:
def current_date
@current_date
end

As Robert indicated, you have not initialized the object so
@current_date is nil.

You can add an initilize method, called automatically by new, that sets
@current_date on object instantiation, i.e:
def initialize
set_current_date
end

Or, if you want the method current_date to always get the current date,
write your own accessor, i.e.:
def current_date
set_current_date
@current_date
end

cheers
 
R

Robert Klemme

aidy said:
Hi Robert,
Thanks for getting back

I am not so sure what I am to set the instance variable to. I was under
the impression that I assigned the instance var here

@current_date = Time.now.localtime.strftime("%d/%m/%Y")

Yes, but you did not invoke that method so the line above was not executed.
And this
attr_reader :current_date

was just a way of doing this

def get_current_date
current_date = @current_date
end

No, it's

def current_date
@current_date
end
No, i don't think so, because I want other methods in the class (for
example:
#current_date_plus_year or something)

It's not clear to me what you intend to do. If you want to retrieve the
value of the data member then it *has* to be set at some point in time.
If you never set it you'll always see nil.

Another remark: rather use @current_data = Date.new - that way you have
the appropriate data type. Don't convert it to a string until you have
to (e.g. when displaying or in method to_s).

Cheers

robert
 
A

aidy

Chirs wrote
As Robert indicated, you have not initialized the object so
@current_date is nil.
You can add an initilize method, called automatically by new, that sets
@current_date on object instantiation, i.e:
def initialize
set_current_date
end
Or, if you want the method current_date to always get the current date,
write your own accessor, i.e.:
def current_date
set_current_date
@current_date
end

Thanks Chris, seems obvious when you point it out, but I am finding it
a little tricky moving from a Basic derived language to Ruby OO.
Thanks Robert.

Aidy
 

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,206
Messages
2,571,069
Members
47,677
Latest member
MoisesKoeh

Latest Threads

Top