How do I remove leading whitespace chars from Ruby HEREDOC?
The <<-
form of heredoc only ignores leading whitespace for the end delimiter.
With Ruby 2.3 and later you can use a squiggly heredoc (<<~
) to suppress the leading whitespace of content lines:
def test
<<~END
First content line.
Two spaces here.
No space here.
END
end
test
# => "First content line.\n Two spaces here.\nNo space here.\n"
From the Ruby literals documentation:
The indentation of the least-indented line will be removed from each
line of the content. Note that empty lines and lines consisting solely
of literal tabs and spaces will be ignored for the purposes of
determining indentation, but escaped tabs and spaces are considered
non-indentation characters.
How to avoid last \n character in heredoc
As sagarpandya82 points out, you will need an additional action (i.e. chomp
) to remove that extra \n
.
You can use chomp
along with heredoc like this:
a = <<~TEXT.chomp
asd
asd
TEXT
#=> "asd\nasd"
Ruby: Split, then remove leading/trailing whitespace in place?
s = "one thing, two things, three things, four things"
s.split(",").map(&:strip)
# => ["one thing", "two things", "three things", "four things"]
In my Ubuntu 13.04 OS,using Ruby 2.0.0p0
require 'benchmark'
s = "one thing, two things, three things, four things"
result = ""
Benchmark.bmbm do |b|
b.report("strip/split: ") { 1_000_000.times {result = s.split(",").map(&:strip)} }
b.report("regex: ") { 1_000_000.times {result = s.split(/\s*,\s*/)} }
end
Rehearsal -------------------------------------------------
strip/split: 6.260000 0.000000 6.260000 ( 6.276583)
regex: 7.310000 0.000000 7.310000 ( 7.320001)
--------------------------------------- total: 13.570000sec
user system total real
strip/split: 6.350000 0.000000 6.350000 ( 6.363127)
regex: 7.290000 0.000000 7.290000 ( 7.302163)
Why does strip not remove the leading whitespace?
Where did the string " Bagsværd"
come from?
It’s likely that the space character at the start of the string is not a “normal” space, but a non-breaking space (U+00A0):
2.0.0p353 :001 > " Bagsværd".strip
=> "Bagsværd"
2.0.0p353 :002 > "\u00a0Bagsværd".strip
=> " Bagsværd"
You could remove it with gsub
rather than strip
:
2.0.0p353 :003 > "\u00a0Bagsværd".gsub(/\A\p{Space}*/, '')
=> "Bagsværd"
This uses the \A
anchor, and the \p{Space}
character property to emulate lstrip
. To strip both leading and trailing whitespace, use:
2.0.0p353 :007 > "\u00a0Bagsværd\u00a0".gsub(/\A\p{Space}*|\p{Space}*\z/, '')
=> "Bagsværd"
string formatting, remove leading chars
We call these "leading" characters, not trailing, since they're at the beginning, but the regex for this is very easy
x.sub(/^[0:]*/,"")
That works exactly as you phrased it: starting at the beginning of the string, remove all the 0s and :s.
How to separate a heredoc from other key-value pairs in a hash literal in Ruby?
This seems to work
{
foo: 123,
bar: <<-HEREDOC,
a longer text
HEREDOC
baz: 456
}
How do I remove everything after first whitespace or certain number of characters?
I prefer to use as simple a solution as I can. Using gsub
is needlessly complex. Either of these will do it:
str = '2008-10-09 20:30:40'
str[/(\S+)/, 1] #=> "2008-10-09"
str[0, 10] #=> "2008-10-09"
What is the Perl syntax to remove extraneous spaces from a Perl heredoc in 'use constant'?
Here's an example that will strip the leading whitespace from each line of a heredoc before assigning it to a constant:
use constant FOO => <<'STRING' =~ s/^\s+//mgr;
abcde
fghij
STRING
print FOO, "\n";
The /r
modifier was new in Perl 5.14, and signifies that the target string isn't modified, but instead that it's copied internally, the copy is modified, and returned as the return value of the substitution operator. For example:
my $new_string = $string =~ s/foo/bar/r;
$new_string
would contain the modified string, and $string
would be left alone.
Applying this to your code, it might look like this:
use constant FIND_OBJECTS_SQL => <<' END_SQL' =~ s/\s+/ /gr;
...
END_SQL
This is very similar to the last example you demonstrated, but adds the /r
modifier so that the substitution has a useful return value, and so that it doesn't attempt to modify the HERE-doc's string literal.
Remove multiple spaces and new lines inside of String
check out Rails squish
method:
http://apidock.com/rails/String/squish
Related Topics
What's the Difference Between Ruby'S Dup and Clone Methods
Look Up All Descendants of a Class in Ruby
Reducing N+1 Queries Using the Bullet and Rspec Gems
Connecting Rails 3.1 With Multiple Databases
How to "Pretty" Format Json Output in Ruby on Rails
How to Match All Occurrences of a Regex
Get the Name of the Currently Executing Method
Trying to Learn/Understand Ruby Setter and Getter Methods
Oo Design in Rails: Where to Put Stuff
Ruby: Inherit Code That Works With Class Variables
Avoiding Applescript Through Ruby: Rb-Appscript or Rubyosa
Query , Can Not Select Column Count
Rails Has_Many :Through Find by Extra Attributes in Join Model
Find_Spec_For_Exe': Can't Find Gem Bundler (≫= 0.A) (Gem::Gemnotfoundexception)