Datetime conversation in ruby [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an api, which has java DateTime type, which returns the following hash:
{"Date":1403592945000}
The actual date value is: "2014-06-24 06:55:45". How do I convert it in Ruby?

Time.at is the function you want to use. However, it seems that your value also contains milliseconds, that in Ruby should passed apart.
Time.at(1403592945000) # wrong
Time.at(1403592945) # => 2014-06-24 08:55:45 +0200

This will work:
Time.at(hash['Date']/1000)

Related

Negative string value in hash ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to use a negative value in a string which is in a hash.
{"amount"=>"-50.01", "currency"=>"CAD"}
If I write transaction.amount I get 0.5001E2, if I use to_f I get 50.01.
I'm trying to extract the -50 value.
Thank you for your help!
Try
transaction['amount']
For example
a = {"amount"=>"-50.01", "currency"=>"CAD"}
Then
a['amount']
returns '-50.01'

is it possible have ruby read 12:34 as a integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm working with a script that only takes integer values. I'm reading from a CSV with a timestamp example of 12:34. Is there a way I get ruby to read it as an integer?
Not sure if you are dealing with a Time object. If so:
str = Time.now.to_s[/\d\d:\d\d:\d\d/]
=> "19:04:53"
str.gsub(":", "").to_i
=> 190453
If it's a string just start with the sub:
"12:34".sub(":", "").to_i
=> 1234

How to use min in formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to min inside the formula to evaluate using eval().
[5,6].min works fine but it doesn't work inside the formula eval([5,6].min*5).
Here is a way using #eval. But why you need #eval ?
eval('[5,6].min * 5')
# => 25
Simply [5,6].min * 5 will work for you.
You don't need eval.
[5,6].min*5.

How to split the first and the last element on a Ruby string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to split a string in 2 parts: the first word and the last one.
I know I need to use the .split method but don't know how.
Is this what you want to do?
first, *_, last = "now is the time for all".split
first #=> "now"
last #=> "all"

Is it possible to pass parameter as argument of a method like this in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to pass parameter to a method like this:
variable = my_method(:parameter)
No quotes, no nothing.Just -> :parameter .
Yes, you can pass a symbol to a method. Example:
puts('hello')
or
puts(:hello)

Resources