[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attribute reader problem

aidy

6/6/2006 2:18:00 PM

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

6 Answers

Robert Klemme

6/6/2006 2:34:00 PM

0

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

aidy

6/6/2006 3:18:00 PM

0

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

Chris Hulan

6/6/2006 3:47:00 PM

0

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

Robert Klemme

6/6/2006 3:50:00 PM

0

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

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

>> 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)

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

aidy

6/7/2006 8:41:00 AM

0

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

Chris Hulan

6/7/2006 5:08:00 PM

0

No problem, I like to help and its more fun than work! 9^)

cheers