is it possible have ruby read 12:34 as a integer? [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 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

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'

How to replace special text in a string in Ruby? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
There is one string like this: 'xxxxxxx#code#xxxxxxxx', I want to replace the code.
Use regex and string.gsub()
2.0.0-p643 :004 > "xxxxxxx#code#xxxxxxxx".gsub(/\#code#/i, "#NEWCODE#")
=> "xxxxxxx#NEWCODE#xxxxxxxx"

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

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"

How do I load a word in Pascal? [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
I want for a variable to be 'name' for example. If I use read(x) and for that variable to be char it will only load the first letter. So if i type 'name' it would be only the first letter.
the word you refer to is actually called string, so you can declare a variable and read string in it:
var
msg:string[10];
begin
readln(msg);
writeln(msg);
end.

Resources