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
> aidy wrote:
> > 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