3 ms·
Ask HN: The importance of heredoc
I wish to ask Rubyists and Shell programmers: Does heredoc still have a place? Is it still worth its syntactic weight? I can see how it would be great for one-off scripts producing a bit of text. But I can picture a number of ways to get almost the same effect (readable one-offs producing text) in a scripting language without complicating the syntax.
- villageidiot 17y agoI've never had a problem with heredoc. I'm curious what you mean when you say "I can picture a number of ways to get almost the same effect . . . without complicating the syntax". Would you care to illustrate your point with an example so we can make the discussion a little bit more concrete?
- stcredzero 17y agoPut the text from a heredoc in a comment with special tags. Have a special method that lets you retrieve the text in the same source file. This puts zero syntactic load on the language, but has much the same effect. You can even have embedded methods, source debugging, regexes. But there's no additional syntax.
- gamache 17y agoOh, heredocs absolutely still have a place. It's the right choice for any multiline string which might contain quoting characters. And what's so complicated about the syntax? Even if you have never seen a heredoc before, it's generally very easy to figure out what's going on from context.
- stcredzero 17y agoThe syntax is a complication to implement, not to just program with.
- firebug 17y agoheredocs are great. I use them all the time for metaprogramming. I don't see them going out of style anytime soon. Not sure what you mean about complicated syntax. I supect you may be new to Ruby and confused by the heredoc sytnax. If so, this post has a review of some of the syntactical issues with heredocs: http://log.gmarik.info/2007/12/rubys-here-document-heredoc-mini.html http://log.gmarik.info/2007/12/rubys-here-document-heredoc-m... Hope that helps.
- stcredzero 17y agoNot confused. Heredoc is one of the things that complicates Ruby syntax from the implementer's POV. Use heredoc for metaprogramming? Now I'm starting to believe in the Lisp dogma.
- mbrubeck 17y agoPerl and PHP also use shell-style heredoc syntax. The main alternative is to provide a newline-friendly quote delimiter, like the """string""" syntax in Python.