Sylar
Sylar

Reputation: 12092

Return Hash Date Value

I have an hash like this: value = {Fri, 14 Oct 2016=>58.0}

How to return the date's value which is 58.0?

Not much help from the docs.

Have tried value["#{DateTime.now}"] but that returns 0.

Upvotes: 1

Views: 304

Answers (2)

Nobita
Nobita

Reputation: 23713

If your key is a DateTime object you should do value[date_object]. So, if you created your hash like:

key = DateTime.now
value = {key=>58.0}

You should access it like: value[key]

If you know the string representation of that DateTime object, you can always convert it to DateTime by using the strptime method

Upvotes: 1

Ursus
Ursus

Reputation: 30071

If that key is a date object try

value[Date.new(2016, 10, 14)]

Upvotes: 1

Related Questions