How can I remove the quotation mark and commas? - ruby-on-rails

I have a date in "12/5/24" format and I am trying to use it in a JavaScript function.
How can I use the date with no quotation marks and with commas?:
Date.UTC(12,5,24)
This is what I am trying to achieve:
date = "12/5/24"
var = date.some_method #=> 12,5,24
Date.UTC(var)

Write as below :
string = "12/5/24"
Date.UTC(*string.split('/').map(&:to_i))

date_array = "12/5/24".split("/").collect(&:to_i)
Date.UTC(date_array[0], date_array[1], date_array[2])

All the split and gsub answers are "wrong", because they fit the anti-pattern "cause the bug, then fix the bug." (Yet they are not "completely wrong", because sometimes that anti-pattern is unfortunately the simplest option!)
Use Time.parse(date).strftime('%m,%d,%y'). That's why you have to escape each y token in a time template with a percent % - so you have a whole string to play with, and can put anything else in it. You could even put the rest of your Javascript in there.

Date.UTC( *"12/5/24".split('/') )

Related

format timezone using XSLT/xpath 2.0

I need to get one date in one format like this:
2020-06-03T06:14:00.000+0100.
following this documentation page [1], I tried to do with this expression, but always get an error:
format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s][Z0000]")
I tried to put with this mask too:
format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s][Z0001]")
but the result is 2020-06-03-14:39:50+02:00
I need to delete the ":" on the offset, ¿Which mask may I use?
[1]https://www.rfc-editor.org/rfc/rfc3339#section-5.6
A workaround for your problem could be splitting the output of format-dateTime into two parts and remove the colon on the second expression:
concat(format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s]"),translate(format-dateTime(current-dateTime(), "[Z0001]"),":",""))
Maybe this works for you.

php str_replace produces strange results

I am trying to replace some characters in a text block. All of the replacements are working except the one at the beginning of the string variable.
The text block contains:
[FIRST_NAME] [LAST_NAME], This message is to inform you that...
The variables are defined as:
$fname = "John";
$lname = "Doe";
$messagebody = str_replace('[FIRST_NAME]',$fname,$messagebody);
$messagebody = str_replace('[LAST_NAME]',$lname,$messagebody);
The result I get is:
[FIRST_NAME] Doe, This message is to inform you that...
Regardless of which tag I put first or how the syntax is {TAG} $$TAG or [TAG], the first one never gets replaced.
Can anyone tell me why and how to fix this?
Thanks
Until someone can provide me with an explanation for why this is happening, the workaround is to put a string in front and then remove it afterward:
$messagebody = 'START:'.$messagebody;
do what you need to do
$messagebody = substr($messagebody,6);
I believe it must have something to do with the fact that a string starts at position 0 and that maybe the str_replace function starts to look at position 1.

Xcode is throwing me an error in swift

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \
(myPlacement.country) \
(myPlacement.postalCode)"
}
. But when I try I get an error
unterminated string literal
and
consecutive statements on a line must be seperated by a ';'
but the guy in the tutorial has done it the exact same way. What's going on?
I'm using the latest swift and and latest xcode 7.2 any help would be apreciated
if I write everything on the same line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
}
it works fine though
if I write everything on the same line like this
Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:
let s = "hello
there"
There is no magic line-continuation character which, placed at the end of the first line, would make that legal.
If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.
You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:
let myAddress = "\(myPlacement.locality) " +
"\(myPlacement.country) " +
"\(myPlacement.postalCode)"
I look your video tutorial carefully. You have a misunderstanding here.
You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.
So, the real code is
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
Please watch it carefully.
And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

string manipulation in ruby on rails

I have a string of the format,
/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk
and i need to edit this string using regular expression that the result string should start from http: ie the resultatnt string should be
http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk
please help
For these types of situations, I prefer to go with readily available tools that will help provide a solution or at the very least will point me in the right direction. My favourite for regex is txt2re because it will output example code in many languages, including ruby.
After running your string through the parser and selecting httpurl for matching, it output:
txt='/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk'
re1='.*?' # Non-greedy match on filler
re2='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))' # HTTP URL 1
re=(re1+re2)
m=Regexp.new(re,Regexp::IGNORECASE);
if m.match(txt)
httpurl1=m.match(txt)[1];
puts "("<<httpurl1<<")"<< "\n"
end
str = "/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk"
str.spl­it("url=")­[1]
Simple Answer
You need to do following
str = "/d.phpsoft_id=369242&url=http://f.1mobile.com/mobile_software/finance/com.mshift.android.achieva_2.apk"
start=str.index('http://')
resultant=str[start,str.length]

NSString to NSDictionary

I have a string (from HTTP Header) and want to split it into a dictionary.
foo = \"bar\",baz=\"fooz\", beta= \"gamma\"
I ca not guarantee that the string is the same every time. Maybe there are spaces, maybe not, sometimes the double quotes are escaped, sometimes not.
So I found the solution in PHP with regular expressions. Unfortunately I can't convert it to work on iOS.
preg_match_all('#('.$key.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))#', $input, $hits, PREG_SET_ORDER);
foreach ($hits as $hit) {
$data[hit[1]] = $hit[3] ? $hit[3] : $hit[4];
}
Can anybody help me converting this to Objective-C?
I met a guy which is kinda RegEx guru. He explained the whole stuff and I got the following (working!!!!) solution in RegEx.
This gives me strings like foo="bar":
(?<=[,\\s])((realm|qop|nonce|opaque)=(?:([\"'])([^\2]+?)\2|([^\\s,]+)))
I then use another RegEx to split it by key and value to create a dictionary.

Resources