Named captures in gsub - ruby

I have the following piece of code, which is working correctly:
line.gsub!(%r{margdat= (\d+/\d+/\d+)}, 'stamp=CONVERT(date, \1,103)')
However, for reasons of code style, I prefer to use named captures. At the moment - it looks too "perl like" for me. I've tried this sort of thing:
line.gsub!(%r{margdat= (?<date>\d+/\d+/\d+)}, "stamp=CONVERT(#{date}, \1,103)")
But it just complains the variable doesn't exist. Any help appreciated.

You want:
'stamp=CONVERT(date, \k<date>, 103)'

Related

Wrap all the strings with single quotas

Let's assume that my template is like a following
string1=${obj.firstString}
string2=${obj.secondString}
number1=${obj.firstNumber}
I'm looking for some automatic way to wrap all my string parameters with single quotas? The expected output is
string1='A'
string2='B'
number1=42
I understand that I can write string1=${"'" + obj.firstString + "'"} , but maybe there is some more conventional way for this requirement...
Thanks a lot!
I would just do this:
string1='${obj.firstString}'
string2='${obj.secondString}'
number1=${obj.firstNumber}
It's a template language, so the basic idea is to make your program look similar to its own output.

DMQL2 Query Syntax for PHRets v2 Seach() to include filter arguments?

(It's been a while since I've been here.)
I've been using the first version of PHRets v1 for years, and understood it well enough to get by, but now I'm trying to understand the advantages of v2.6.2. I've got it all installed and the basics are working fine. My issues are pretty much with comprehending fine points of query syntax that goes into the rets=>Search() statement. (I'm much more familiar with SQL statements). Specifically, I'd like to have a query return a list of properties, EXCLUDING those which already have the status of "Sold".
Here's where I am stuck: If I start with this
`$results = $rets->Search('Property', 'A','*',['Select' => 'LIST_8,LIST_105,LIST_15,LIST_19,listing_office_shortid']);`
That works well enough. BUT I'd like to fit in a filter like:
"LIST_15 != Sold", or "NOT LIST_15=Sold"...something like that. I don't get how to fit/type that into a PHRets Search().
I like PHRets but it is so hard to find well-organized/complete documentation about specific things like this. Thanks in advance.
As in my comment above I've figured out that the filter goes in the third argument position ('*', as in the original question). The tricky thing was having to find a specific "sold" code for each class of properties and placing it in that position like so: '(LIST_15=~B4ZIT1Y75TZ)', (notice the =~ combination of characters that means "does not equal" in this context). I've found the code strings for each of the property types (not clear WHY they would need to be unique for each type of property: "Sold" is Sold for any type, after all) but the correct code for a single-family residential property (type 'A' ...at least for the MLS in which I have to search is:
$results = $rets->Search('Property', 'A','(LIST_15=~B4ZIT1Y75TZ)',['Select' => 'LIST_8,LIST_105,LIST_15,LIST_19,listing_office_shortid']);
(again, the code to go with LIST_15 will be different for the different types of properties.) I think there is a better answer that involves more naturalistic language, but this works and I guess I will have to be satisfied with it for now. I hope this is of some use to anyone else struggling with this stuff.

How do I match a full link in capybara?

In Capybara+Rspec, I can check that a link is missing:
response.body.should_not have_link("link_content")
This is fine, but unfortunately the test fails for when "link_content" partially matches a link, such as "this_is_a_long_link_content". How can I change the test to make it pass in this case?
(That is, the matcher should not partially match, it should only fully match).
You can also use the following workaround:
response.body.should_not have_xpath("//a[normalize-space(text())='link_content']")
This is whitespace-agnostic and therefore a little more flexible than the raw HTML approach.
From the docs:
If all else fails, you can also use the page.html method to test against the raw HTML:
This works for me:
page.html.should match('>\s*Log in\s*</a>')
page.html.should_not match('>\s*link_content\s*</a>')
Note that the argument to match can a regular expression. That means that you can make the solution whitespace-agnostic by simply adding \s*.

TextMate Snippet: How to uppercase the first letter in a regexp

How can I make the first letter upper case in the following:
${1:${TM_FILENAME/[\.php]+$//}}
Basically if the filename is "welcome.php", I'd like it to write out "Welcome". This at the moment writes "welcome" (lower case w).
Try the following snippet. Works for me.
${TM_FILENAME/(.*?)(\..+)/\u$1/}
For some reason the above example gives you weird charectors when using it ... it took me a while but the solution below works if you are still looking. It has two solutions here for you the first one is for actual textmate on mac:
${TM_FILENAME/(^.)(.*?)(\.php)/(?1:\U$1)(?2:)(?3:)/}
The next is if you are using e Texteditor on PC:
${TM_FILENAME/(^.)(.*?)(\.php)/(?1:)(?2:$2)(?3:)/}
I hope this helps you if you haven't already.
TM_FILENAME not works for windows?

Convert Ruby code to string

I am refactoring some code that I did not write, and I found a line that looks like this (it is much longer, i used just a little bit for this example):
system("rubyw -e \"require 'win32ole'; #autoit=WIN32OLE.new('AutoItX3.Control');")
To increase readability, I refactored it to
do_something =
"rubyw -e \"
require 'win32ole'
#autoit=WIN32OLE.new('AutoItX3.Control')"
system do_something
Then I wanted to make some changes, but since the code I am working on is in a string, I lose syntax highlighting, parenthesis matching and all that good stuff.
Is there an easy way to write some code outside of the string and then convert it to string?
I have searched the web and stackoverflow, but could not find the answer.
For more information, take a look at original code at bret/watir (Watir::FileField#set, line 445), and my fork at zeljkofilipin/watir (lines 447-459).
You can use the following syntax:
do_something = <<SOMETHING
rubyw -e
require 'win32ole'
#autoit=WIN32OLE.new('AutoItX3.Control')
SOMETHING
Apparently it's a heredoc! You can find another example here(doc).
That's not to say that the command won't freak out about having line breaks in there. However you could likely run it by system do_something.split(/\r\n/).join('') or something like that.
Well, you could just put the code into an external file and load that.
Alternatively, if I remember correctly, Ruby files may contain DATA sections:
# Sample ruby file:
puts DATA.readline()
__END__
foo bar
This should print "foo bar". You could put your code in the data segment. With any luck, your editor will still provide syntax highlighting for the DATA segment.
Ruby2ruby sounds kind of like what you're asking.
http://seattlerb.rubyforge.org/ruby2ruby/

Resources