gsub variable regardless of case - ruby

I have the following code which will set a highlight for any parts of the phrase where the word is. I would like this to function regardless of case but I am struggling with the syntax. I believe I need to add /i somewhere, but I do not know where.
params.fetch('phrase').gsub(params.fetch('word'),
'<span class="ko-highlight">' + params.fetch('word') + '</span>')

This should do the trick
params.fetch('phrase').gsub(/#{params.fetch('word')}/i, '<span class="ko-highlight">' + params.fetch('word') + '</span>')

Related

How do I comment out a block of code in Stan?

This feels like a duplicate question, but I can't find it anywhere here or elsewhere!
So, in R, you can comment out a block of code with Ctrl + Shift + C/Cmd + Shift + C. Does anyone know how to perform the equivalent action in Stan? All I know is that Stan uses // in place of R's #. I've tried Ctrl + Shift + //Cmd + Shift + /, to no avail.
Thanks so much for any help! :)
You can create a comment within your Stan code by using the following:
/* this is a comment block
as opposed to a single
line block */
Please see, https://mc-stan.org/docs/2_18/reference-manual/bracketed-comments.html

Interpolation with conditionals

I want to insert data into a string via interpolation. I want to check if #call.transferred_from is nil, and if so, output #call.transfer_from_other; else output #call.transferred_from.try(:facility_name) along with #call.transferred_from.try(:facility_address).
Here is my code example:
"#{if #call.transferred_from.nil? #call.transfer_from_other else #call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address) end}"
Doing this gives me the following error:
syntax error, unexpected keyword_else, expecting keyword_then or ';' or '\n'
I'm not sure where to go. Any help would be appreciated.
Update: 08/04/14
I moved the conditional into a private controller method as follows:
def transfer_from_address
if #call.transferred_from.nil?
#call.transfer_from_other
else
#call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
end
end
Then I call the following using string interpolation.
#{transfer_from_address}
This seems to work, but I'm not sure that it's proper Ruby.
I know this is not really answering your question, but I'd caution about putting this much logic in an interpolation. While its totally doable, it makes your code very hard to understand.
The fundamental issue I see with your particular issue is you're trying to return 2 things somehow, yet you're just putting both of them next to eachother which is not valid ruby.
Assuming this is in an interpolation you'd want to somehow return them together ..
#{
#call.transferred_from.nil? ?
#call.transfer_from_other :
#call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
}
I'd really suggest you move this into a variable or a method tho .. and just reference it in the interpolation.
This could look something like:
facility_name_and_address = #call.transferred_from.nil? ? #call.transfer_from_other : #call.transferred_from.try(:facility_name) + ' ' + #call.transferred_from.try(:facility_address)
{
:body => facility_name_and_address
}
If I understand what you are trying to do, I would suggest adding a method to #call which does the job:
class Call
def transfer_text
return transfer_from_other if transferred_from.nil?
"#{transferred_from.try(:facility_name)} #{transferred_from.try(:facility_address)}"
end
end
Then simply calling #call.transfer_text should provide the needed text.
If you want to be more sophisticated, and you don't want trailing white-space in case facility_name or facility_address are nil, you can create a list of them, and join them with white space:
[transferred_from.try(:facility_name), transferred_from.try(:facility_address)].compact.join(' ')
This will make sure spaces will be only between to non-nil elements. If both are nil, and empty string will be the result (rather than a space), and if one is nil, it won't have a leading/trailing space.
why not using
:body => "#{#call.transferred_from.nil? ? #call.transfer_from_other : #call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address)"
but anyway I would not use this compact syntax for better maintainability
You just need to put either a semicolon or then right after the condition.
if #call.transferred_from.nil?; #call.transfer_from_other ...
But in your case, there is not much point in putting the entire condition inside a string interpolation. It is better to do the condition outside the string.
By the way, if you fix your first error, then you might encounter the next error:
#call.transferred_from.try(:facility_name) #call.transferred_from.try(:facility_address)
To fix that as well, I think you should do
#call.transferred_from.instance_eval{|e| e.nil? ?
#call.transfer_from_other.to_s :
"#{e.try(:facility_name)} #{e.try(:facility_address)}"
}

How do I parse a quoted string inside another string?

I want to extract the quoted substrings from inside a string. This is an example:
string = 'aaaa' + string_var_x + 'bbbb' + string_var_y
The output after parsing should be:
["'aaaa'", "'bbbb'"]
The initial solution was to string.scan /'\w'/ which is almost ok.
Still I can't get it working on more complex string, as it's implied that inside '...' there can be any kind of characters (including numbers, and !##$%^&*() whatever).
Any ideas?
I wonder if there's some way to make /'.*'/ working, but make it less greedy?
Lazy should fix this:
/'.*?'/
Another possibility is to use this:
/'[^']*'/
An alternate way to do it is:
>> %{string = 'aaaa' + string_var_x + 'bbbb' + string_var_y}.scan(/'[^'].+?'/)
#=> ["'aaaa'", "'bbbb'"]
String.scan gets overlooked a lot.

Flatten an array of strings in Ruby

What's the best idiomatic (cleanest) way to convert an array of strings into a string, while keeping the enclosing quotes for each elements.
In other words, from this:
a = ["file 1.txt", "file 2.txt", "file 3.txt"]
I'd need to get this
"'file 1.txt' 'file 2.txt' 'file 3.txt'"
Single and double quotes could be interchanged here.
The best ways I know of is by using map and inject/reduce.
eg: a.map{|dir| "'" + dir + "'"}.join(' ')
eg2: a.reduce("'"){|acc, dir| acc += dir+"' "}
Performance could be improved by avoiding temp string creation (+ operator). That's not my main question though. Is there a cleaner more concise way to achieve the same result?
Shorter doesn't always mean simpler. Your first example was succinct, readable, and easily changeable, without being unnecessarily complex.
a.map { |s| "'#{s}'" }.join(' ')
Try out
"'#{a.join("' '")}'"
Or if golfing
?'+a*"' '"+?'
Try this:
"'" + a.join("' '") + "'"
"'"+a*"' '"+"'"
or
"'#{a*"' '"}'"
or
a.to_s[1...-1].gsub /",?/,"'"

Visual Studio Find and Replace Variables

I am trying to replace a two letter state abbreviation with text then the abbreviation.
Eventually I want to find and replace the rest. How do I capture the value found? .... I tried \1 and {1}
AL 32.2679134368897 -86.5251510620117
AR 35.2315113544464 -92.2926173210144
AZ 33.3440766538127 -111.955985217148
CO 39.7098631425337 -104.899092934348
if( usState == "AZ") dpos= "33.4736704187888" + " " + "-112.043138087587";
if( usState == "CA") dpos= "36.0783581515733" + " " + " -119.868895584259";
if( usState == "CO") dpos= "39.8950788035537" + " " + " -104.831521872318";
if( usState == "CT") dpos= "41.6001570945562" + " " + " -72.6606015937273";
Update
$1 does not work.
I am finding: [A-Z][A-Z]
replacing with: if( usState == "$1
Oddly enough, Visual Studio Regular Expressions are different than normal .Net regular expressions. They have a slightly different syntax for tags and replaces. In order to tag a piece of text for later matching you must wrap it in braces {}. Then you can use \n in the replacement strings where n is the nth tagged expression. For your scenario here are the strings you should use
Find: {[A-Z][A-Z]}
Replace: if( usState == "\1")
My regex matcher matches $1. Try that.
I might not have understood your problem, but why don't you record a temporary macro to do the transformation?
Since this questions seems to be a duplicate of https://stackoverflow.com/a/3147177/154480 but I found this one first: since Visual Studio 2012, you can use (pattern) and $1. As an example for this specific question, find ([A-Z]{2}) by if( usState == "$1")
Enclose the [A-Z][A-Z] within parentheses, which captures it; then, use \1 in your replacement string to refer to the capture.

Resources