def foo
return <<-END
This is a test.
END
endresults in a function foo() that returns " This is a test.\n". This is probably not what the author intended the result to be. There are solutions that use regular expressions at run-time to build a here-doc without leading whitespace. For example [ruby-talk:61677],
def foo
return %Q{
This is a test.
}.gsub!(/^\s*/, '')
endwhich strips all leading whitespace (and therefore probably undesirable). Another option [ruby-talk:61446]:
def tabto(n)
if self =~ /^( *)\S/
tab(n - $1.length)
else
self
end
end
def foo
return %Q{
This is a test.
}.tabto(0)
end
which is good but IMO is not intuitive when reading code that uses it.
As long as we are missing extended here docs, users will continue to produce ad-hoc solutions like the above. we need a solution that occurs at parse time and is intuitive and easy to use.
p <<-|EOS
| foo
| bar
EOS
# => " foo\n bar\n".The parser should understand the <<-| syntax to indicate an extended here doc in which the | character is used to indicate the start of the line.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog