This question already has answers here:
What is <<- in ruby?
(2 answers)
Closed 7 years ago.
This bit of code is taken from Ryan Bates' Railscast episode 343 on full-text search in PostgreSQL. I would like to understand it, but I can't find anything on the <<- operator (if it even is an operator). Can someone please point me to somewhere I can learn about this?
rank = <<-RANK
ts_rank(to_tsvector(name), plainto_tsquery(#{sanitize(query)})) +
ts_rank(to_tsvector(content), plainto_tsquery(#{sanitize(query)}))
RANK
It is a multiline String in ruby, the contents are interpolated and then executed in PostgreSQL. This is a standard way to run scripts on the command line. I use it to write AWS Scripts from within Capistrano.
It uses the here-doc syntax.
http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html
http://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Here+Documents
It's official name is a heredoc and there are two different ways you can use them.
One is how you have it laid out where the start will be <<-NAME and the end will simple be NAME.
The other way you can do it is <<NAME but when closing you have to make sure there are no spaces before NAME on the line. Some example code below to show the difference.
def sample_method
heredoc1 = <<-NAME
This is a sample heredoc
NAME
heredoc2 = <<OTHERHEREDOC
Both of these are the same thing
OTHERHEREDOC
end
Both of these will work as heredocs, but as you can see the second one looks a little uglier. Choose whichever you prefer when using them yourself, but make sure to pay attention to white space and the end of string delimiter if you don't include the dash.
Related
Problem
I'm writing an essay/documentation about an application. Within this doc there are a lot of code-words which I want to be highlighted using a different font. Currently I work with:
{\fontfamily{cmtt}\selectfont SOME-KEY-WORD}
Which is a bit of work to use every time.
Question
I'm looking for a way to declare a list of words to use a specific font within the text.
I know that I can use the listings package and define morekeywords which will be highlighted within the listings-environment but I need it in the text.
I thought of something like this:
\defineList{\fontfamily{cmtt}}{
SOME-KEY-WORD-1,
SOME-Key-word-2,
...
}
EDIT
I forgot to mention that I already tried something like:
\def\somekeyword{\fontfamily{cmtt}\selectfont some\_key\_word\normalfont}
which is a little bit better then the first attempt but I still need to use \somekeyword in the text.
EDIT 2
I came upon a workaround:
\newcommand{\cmtt}[1]{{\fontfamily{cmtt}\selectfont #1\normalfont}}
It's a little better then EDIT but still not the perfect solution.
Substitution every time a word occurs, without providing any clues to TeX, might be difficult and is beyond my skills (though I'd be interested to see someone come up with a solution).
But why not simply create a macro for each of those words?
\newcommand\somekeyword{\fontfamily{cmtt}\selectfont SOME-KEY-WORD}
Use like this:
Hello, \somekeyword{} is the magic word!
The trailing {} are unfortunately necessary to prevent eating the subsequent whitespace; even the built-in \LaTeX command requires them.
If you have very many of these words and are worried about maintainability, you can even create a macro to create the macros:
\newcommand\declareword[2][]{%
\expandafter\newcommand%
\csname\if\relax#1\relax#2\else#1\fi\endcsname%
{{\fontfamily{cmtt}\selectfont #2}}%
}
\declareword{oneword} % defines \oneword
\declareword{otherword} % defines \otherword
\declareword[urlspy]{urls.py} % defines \urlspy
...
The optional argument indicates the name of the command, in case the word itself contains characters like . which cannot be used in the name of a command.
So i have this variable:
a = [[C:\aaa\aaa\aa\bbb\ccc\ddd]]
And i need to end up here:
a = [[ccc\ddd]]
Note that the path (the aaa,ccc and ddd folders) might be different from time to time, but the word "bbb" is always gonna be there and thats what i´d like to use to start chopping the text (from the end of the word not from the beginning)
I´ve been reading some string tutorials and everything i tried just doesnt work (pretty new to scripting here). I think the "\" character messes things up.
Whats the best way to deal with this? Thaaaanks!
This is a good time to make use of patterns.
Information on that here: understanding lua patterns
With a pattern you could use string.match to flexibly capture the part of the string you want
a ="C:\\aaa\\aaa\\aa\\bbb\\ccc\\ddd"
print(string.match(a, "bbb\\(.*)"))
What i want to do is to make an obfuscate id when i show a user url, i want to have an obfuscate id with exactly 16 characters in length, and without collision, so for example instead of
http://localhost:3000/users/2
i want something like this :
http://localhost:3000/users/3a5643f943cc3a44
i already find the same question in other posts here and here and also others... but can't find an answer of what i want exactly.
===========
Update:
===========
It's been some years now since I posted this question, I just want to mention that there is a super helpful gem called hashids which I use in all my recent projects, it's straight forward and easy to use :)
I would recommend using SecureRandom.urlsafe_base64. This following related question can also help: Best way to create unique token in Rails?
16 characters is the default length (but the length is variable, as can be seen in the documentation: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html#method-c-urlsafe_base64)
I am using Ruby on Rails 3.1.0 and I am refactoring/reordering my source code so to make that more readable. I am using a code area with a "right margin" of 120 spaces (that is, I have 120 characters of space to write the code).
The following image should make it clear what I mean:
Note: the image is from the NetBeans IDE software.
Now, in my source code I have long constant and method names that come out from that limit of 120 spaces. For example:
a_my_very_long_variable_name_stated_just_for_testing_purposes = A_MY_VERY_LONG_CONSTANT_NAME_STATED_JUST_FOR_TESTING_PURPOSES
a_my_very_long_method_name_stated_just_for_testing_purposes(a_my_very_long_parameter_name_stated_just_for_testing_purposes_1, a_my_very_long_parameter_name_stated_just_for_testing_purposes_2)
How should I refactor above codes? That is, how can I contain those in 120 spaces? What are best Ruby practices for these issues?
P.S.: I have considered the possibility of shortening constant and method names but at the moment is not applicable.
You can write it like this:
a_my_very_long_variable_name_stated_just_for_testing_purposes =
A_MY_VERY_LONG_CONSTANT_NAME_STATED_JUST_FOR_TESTING_PURPOSES
a_my_very_long_method_name_stated_just_for_testing_purposes(
a_my_very_long_parameter_name_stated_just_for_testing_purposes_1,
a_my_very_long_parameter_name_stated_just_for_testing_purposes_2
)
If the last token on the line is an operator or a comma you can break statement into multiple lines. Alternatively, you can use \ to continue on the next line.
A pet peeve of mine is the use of double square brackets for Part rather than the single character \[LeftDoubleBracket] and \[RightDoubleBracket]. I would like to have these automatically replaced when pasting plain-text code (from StackOverflow for example) into a Mathematica Notebook. I have been unable to configure this.
Can it be done with ImportAutoReplacements or another automatic method (preferred), or will I need use a method like the "Paste Tabular Data Palette" referenced here?
Either way, I am not good with string parsing, and I want to learn the best way to handle bracket counting.
Sjoerd gave Defer and Simon gave Ctrl+Shift+N which both cause Mathematica to auto-format code. These are fine options.
I am still interested in a method that is automatic and/or preserves as much of the original code as possible. For example, maintaining prefix f#1, infix 1 ~f~ 2, and postfix 1 // f functions in their original forms.
A subsection of this question was reposted as Matching brackets in a string and received several good answers.
Not really an answer, but a thread on entering the double [[ ]] pair (with the cursor between both pairs) using a single keystroke occurred a couple of weeks ago on the mathgroup. It didn't help me, but for others this was a solution apparently.
EDIT
to make good on my slightly off-topic first response here's a pattern replacement that seems to do the job (although I have difficulties myself to understand why it should be b and not b_; the latter doesn't work):
Defer[f[g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]]]] /.
HoldPattern[Part[b, a_]] -> HoldPattern[b\[LeftDoubleBracket]a\[RightDoubleBracket]]
I leave the automation part to you.
EDIT 2
I discovered that if you add the above rule to ImportAutoReplacements and paste your SO code in a notebook in a Defer[] and evaluate this, you end up with a usable form with double brackets which can be used as input somewhere else.
EDIT 3
As remarked by Mr.Wizard invisibly below in the comments, the replacement rule isn't necessary. Defer does it on its own! Scientific progress goes "Boink", to cite Bill Watterson.
EDIT 4
The jury is still out on Defer. It has some peculiar side effects, and doesn't work well on all expressions. try the "Paste Tabular Data Palette" in the toolbag question for instance. Pasting this block of code in Defer and executing gives me this:
It worked much better in another code snippet from the same thread:
The second part is how it looks after turning it in to input by editing the output of the first block (basically, I inserted a couple of returns to restore the format). This turns it into Input. Please notice that all double brackets turned into the correct corresponding symbol, but notice also the changing position of ReleaseHold.
Simon wrote in a comment, but declined to post as an answer, something fairly similar to what I requested, though it is not automatic on paste, and is not in isolation from other formatting.
(One can) select the text and press Ctrl+Shift+N to translate to StandardForm