A heredoc in Ruby can be used like this:
puts <<-END.downcase
SOME TEXT
END
A heredoc in Crystal cannot. This should be changed.
Wow, I didn't know you could do that in Ruby.
You can also do this:
puts(<<-END
SOME TEXT
END
.downcase)
to get the same effect, so I don't think it's worth implementing that Ruby feature. Let's keep the language simple.
Except that it won't work if you omit the redundant line break after END
.
@asterite thanks for closing this perfectly reasonable request without discussion or explanation.
It's...it's one line break...why the big deal?
The whole point of a heredoc is to _avoid_ having to wrap it all in parens and split calls over lines. If you're going to do that, just remove heredocs from the language completely, and use %{ ... }
instead.
Heredocs are useful. They provide a clear and uncluttered way to embed arbitrary paragraphs of text into a code file; but it becomes far less useful when you have to wrap it in parens and line breaks.
+1 for stugol
i have code in ruby something like this :)
items = Item.find_by_sql(<<-SQL, item_id)
select * from items where id = ?
SQL
Ruby also allows pretty complex expressions using more than one heredoc on the same line:
puts <<-TITLE.downcase + "Name: #{<<-NAME}"
Something
TITLE
John Doe
NAME
And my favorite Ruby Quine, doesn't work on Crystal:
puts <<2*2,2
puts <<2*2,2
2
I don't know how hard it is to parse, given that it doesn't quite work like a string expression that has a start, a middle content and a terminator. But it is indeed useful for using heredocs into any non trivial expression. Putting chained method calls after the heredoc terminator makes reading harder if the heredoc is not very small.
We can write the ruby code:
puts <<-HERE.gsub /[aeiou]/, "*"
apple
banana
HERE
Into Crystal like this:
puts (<<-HERE
apple
banana
HERE
).gsub /[aeiou]/, "*"
Or:
tmp = <<-HERE
apple
banana
HERE
puts tmp.gsub /[aeiou]/, "*"
I want to keep my code pretty, but the first case is too ugly.
And also keep my code clean and simple that I don't want to create the tmp var in the second case.
+1 for stugol
i have code in ruby something like this :)items = Item.find_by_sql(<<-SQL, item_id) select * from items where id = ? SQL
馃く
Most helpful comment
+1 for stugol
i have code in ruby something like this :)