Ruby: Transform a string of time to xsd:datetime conforming string? - ruby

I have this: "2013-02-23 18:06:00 UTC"
and need this: "2013-02-23T18:06:00Z"
to conform to this: http://books.xmlschemata.org/relaxng/ch19-77049.html
Does anyone know of a good library/tool/method in Ruby to do this without having to write some transformation method?

Check out the rubydocs for Datetime here. There's a method to convert into ISO8601.

Within Rails:
DateTime.now.utc.strftime()
=> "2012-08-22T17:55:12+00:00"

Related

remove `\"` from string rails 4

I have params like:
params[:id]= "\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\""
And i want to get expected result as below:
"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
How can I do this?
You can use gsub:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".gsub("\"", "")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
Or, as #Stefan mentioned, delete:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".delete("\"")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
If this is JSON data, which it could very well be in that format:
JSON.load(params[:id])
This handles things where there's somehow escaped strings in there, or the parameters are an array.
Just Use tr!
params[:id].tr!("\"","")
tr! will also change the main string
In case you do not want to change main string just use :
params[:id].tr("\"","")
Thanks Ilya

How to use gsub regex in ruby

I want to remove some part of string from using ruby regex:
value = localhost:8393/foobar/1 test:foobartest
I want to remove "test" from my string [localhost:8393/foobar/1 test:foobartest] and rest of the value so that output should look like:
localhost:8393/foobar/1
How to do this in ruby? Can you share some sample code to achieve this?
Appreciated your help in advance!
Thanks!
I would do something like this:
value = 'localhost:8393/foobar/1 test:foobartest'
value.split.first
#=> "localhost:8393/foobar/1"
Or if you want to use an regexp:
value.sub(/ test.*/, '')
"localhost:8393/foobar/1"

How to evaluate a string in its template with given values in ruby

there is a string like this and it is stored in a file
#{date}abcde.doc
I want to be able to read this string and replace #{date} with
Date.today.strftime("%Y%m%d")
Is there any way to parse the template and do the evaluation? Thanks in advance!
Yes, however...
It would be easier if you used hash replacement, like this:
s = "%{date}abcde.doc"
s % { date: Time.now.strftime(etc) }
Or just use ERb.
As-is you're using string interpolation so it would need to be evaled, I think.

Ruby - convert string to date

I have a string like "2011-06-02T23:59:59+05:30".
I want to convert it to date format and need to parse only the date, "2011-06-02".
For Ruby 1.9.2:
require 'date' # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s
require 'date'
d = Date.parse("2011-06-02T23:59:59+05:30")
d.strftime("%F")
Simplies way is
require 'date'
date = "2011-06-02T23:59:59+05:30".gsub(/T.*/, '')
DateTime.parse(date)
Time.parse() should allow you to parse the whole date time. Then you can use time.strftime( string ) to format that as just a date in a string.
date = Time.parse("2011-06-02T23:59:59+05:30")
date_string = time.strftime("%y-%m-%d")
of
date_string = time.strftime("%F")
(see Ruby Doc for Time for more output string formats)
The above should work if you want a string; if you want a date object to handle then the ruby Date class can help you handle it but I belive that everything still needs to be done with Time objects; see Ruby Doc for Date for details of the Date class.
Hope that helps, let me know if I have headed off in the wrong direction with my answer.

Reflection in Ruby. Instantiate an object by given class name

I came to ruby from PHP.
How could i do the next thing in ruby?
$className = 'ArrayObject';
$arrayObject = new $className();
You can do this:
arrayObject = Object::const_get('Array').new
You can also use the following if you are using Ruby on Rails:
array_object = "Array".constantize.new
If you have a class, like for example String:
a = String
a.new("Geo")
would give you a string. The same thing applies to other classes ( number & type of parameters will differ of course ).

Resources