How to evaluate a string in its template with given values in ruby - 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.

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

Freemarker - default value for variable that may be missing or blank?

In Freemarker, I want to treat something that may be missing (not defined on given object or in given Map), have a null value, be an empty string "" or a string with whitespace only (" \t\n"), AKA blank string. Or a real value like "foo".
In case it's anything besides a non-blank string, I want the expression to return a default value.
How can I handle this in Freemarker?
So far, this is what I use:
${ obj.propery???then(obj.property?trim!"default") }
But I can imagine something more ellegant, like
${ obj.property!!?trim!"default" }
and even with trimming (which is quite common operation for templates):
${ obj.property!!!"default" }
Is there something such in Freemarker? (Besides coding my own method or macro.)
There's nothing in FreeMarker for that, at least not in 2.3.24. I think the best way to address that is wiriting a trimToNull function, and then you can write trimToNull(obj.property)!"default" and trimToNull(obj.property)??, etc.
I had the same challenge and came to this solution:
${ (obj.property!"")?trim }
It also trims the empty string but is easier to read than writing an function or a if.

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"

Use embedded string as variable name

I have a YAML file that uses the encoding __firstname__ as a placeholder which signifies that an existing method firstname should be used, rather than the literal string in a subsequent process.
I am trying to understand the most ruby way to to do this. Basically, I need to extract the part between the underscores and send it to an object. Here is pseudocode:
variable = '__firstname__'
if variable is prefixed and suffixed with underscores
result = object.send(variable.removeunderscores)
else
result = variable
end
puts result
I was about to write this procedurally like this, but this is the type of thing that I think ruby can less clunkily if only I knew the language better.
What is a clean why to write this?
There's nothing wrong with verbose code if it's clear to read IMO.
I'd do something like this using String#start_with? and String#end_with?:
variable = '__firstname__'
if variable.start_with?("__") && variable.end_with?("__")
result = object.send(variable[2...-2])
else
result = variable
end

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

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"

Resources