get milliseconds since 1970 from string
Try
require 'date'
DateTime.parse("2016-08-12 06:13:24 UTC").strftime('%Q')
Get first 3 integer
in rails console
something like
=> 92774.to_s.first(3).to_i
=> 927
in irb
=> 92774.to_s.chars.first(3).join.to_i
=> 927
How do i print the date and time of specific lines that contain a certain key word using Ruby?
One way to do it would be like so (within a block that's iterating over the lines in the file, obviously):
if line.include?('MAY_DAY')
puts line[0..14]
end
Since the date information (which is what you want output) appears in the same position and is the same length in every line, we don't bother doing any parsing of the text for the output - just spit out the first 15 characters.
I'm tempted to try to compress all of this into a single regular expression, but this ought to work. Obviously, you could do something other than print out the date within the conditional, and if you wanted to work with it as a date, you could pass it to DateTime.parse()
(just remember to require 'date'
first).
Convert times created by Javascript's getTime() method to ruby Time objects
You can use the Time.at method, and divide the JavaScript timestamp value by 1000, since the at method, uses seconds instead of milliseconds, for example:
jsTime = 1252268867928
print Time.at(jsTime/1000)
# Sun Sep 06 20:27:47 +0000 2009
Ensuring a unique number to identify something
If you need unique identifier, you should make such. For example, SecureRandom.uuid
:
p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
(I assume that PayPal accepts arbitrary strings and doesn't require actual integers)
Related Topics
In Rails - Is There a Rails Method to Convert Newlines to <Br>
Rails 3 - No Such File to Load -- Openssl
Best Way to Highlight Current Page in Rails 3? - Apply a CSS Class to Links Conditionally
Gem.Source_Index Is Deprecated, Use Specification. Should I Re-Install Gem or Rails
What's the Difference Between Gets.Chomp() VS. Stdin.Gets.Chomp()
Finding Out Current Index in Each Loop (Ruby)
Get the Value of an Instance Variable Given Its Name
Rails: Postgres Permission Denied to Create Database on Rake Db:Create:All
How to Make Instance Variables Private in Ruby
How to Compare Strings Ignoring the Case
How to Print Out the Contents of an Object in Rails for Easy Debugging
What's the Difference Between Gem_Home and Gem_Path
Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby