# Problem with HERE document as first parameter (parser bug?)

**URL:** <https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086>\
**Category:** Ruby\
**Created:** [July 11, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086 "2007-07-11T13:48:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/1 "2007-07-11T13:48:11Z")

</div>

I have problems passing a HERE document as the first parameter  
to a function expecting more than 1 parameter. Example:

# This is file here1.rb

def q(a,b)  
end  
q(\<\<-END  
line1  
line2  
END  
,‘x’)

Executing this program yields the error messages

./here1.rb:9: syntax error, unexpected ‘,’, expecting ‘)’  
,‘x’)  
^  
./here1.rb:9: syntax error, unexpected ‘)’, expecting $end

Using a temporary variable to hold the content of the HERE  
string works fine though:

temp=\<\<-END  
line1  
line2  
END  
q(temp,‘x’)

Bug in Ruby? Or do I misunderstand something in the workings of the  
parser?

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 1:53pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/2 "2007-07-11T13:53:28Z")

</div>

Ronald F. wrote:

> I have problems passing a HERE document as the first parameter  
> to a function expecting more than 1 parameter. Example:
> 
> # This is file here1.rb
> 
> def q(a,b)  
> end  
> q(\<\<-END  
> line1  
> line2  
> END  
> ,‘x’)
> 
> Executing this program yields the error messages
> 
> ./here1.rb:9: syntax error, unexpected ‘,’, expecting ‘)’  
> ,‘x’)  
> ^  
> ./here1.rb:9: syntax error, unexpected ‘)’, expecting $end
> 
> Using a temporary variable to hold the content of the HERE  
> string works fine though:
> 
> temp=\<\<-END  
> line1  
> line2  
> END  
> q(temp,‘x’)
> 
> Bug in Ruby? Or do I misunderstand something in the workings of the  
> parser?
> 
> Ronald

Not a bug in the parser, just wrong usage.  
q(\<\<-END, ‘x’)  
line1  
line2  
END

That will work.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/3 "2007-07-11T14:27:39Z")

</div>

On Jul 11, 2007, at 6:53 AM, Stefan R. wrote:

> > END
> > 
> > Bug in Ruby? Or do I misunderstand something in the workings of the  
> > That will work.
> 
> Regards  
> Stefan

While it works, it is, IMHO, ugly and a little obfuscated.  
It is much better to simply assign the heredoc to a variable, and put  
the variable name in the function parameter.  
I know it is a correct form, but some times linguistically correct is  
not always good for you. (most people know what I mean if I mention C )

John J.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/4 "2007-07-11T14:56:41Z")

</div>

2007/7/11, Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ,‘x’)
> 
> Executing this program yields the error messages
> 
> ./here1.rb:9: syntax error, unexpected ‘,’, expecting ‘)’  
> ,‘x’)  
> ^  
> ./here1.rb:9: syntax error, unexpected ‘)’, expecting $end

I believe you have got the order wrong. Do it like this:

$ ruby -e 'def f(a,b) p a,b end

> f(\<\<XXX, 123)  
> foo  
> bar  
> XXX’  
> “foo\nbar\n”  
> 123

> Bug in Ruby? Or do I misunderstand something in the workings of the  
> parser?

The latter, see above. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/5 "2007-07-11T16:10:57Z")

</div>

2007/7/11, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > ```
> > line2
> > 
> > ```
> 
> That will work.
> 
> Regards  
> Stefan
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> HERE document as second parameter works:

irb(main):072:0\> def q(a,b)  
irb(main):073:1\> puts a  
irb(main):074:1\> puts b  
irb(main):075:1\> end  
=\> nil  
irb(main):076:0\> q(‘1’,\<\<-END  
irb(main):077:1" 2  
irb(main):078:1" END  
irb(main):079:1\> )  
1  
2  
=\> nil

If correctness of usage depends on parameters order - is this a bug?

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 11, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/problem-with-here-document-as-first-parameter-parser-bug/109086/6 "2007-07-11T20:29:36Z")

</div>

Valeri Mytinski wrote:

> irb(main):072:0\> def q(a,b)  
> irb(main):073:1\> puts a  
> irb(main):074:1\> puts b  
> irb(main):075:1\> end  
> =\> nil  
> irb(main):076:0\> q(‘1’,\<\<-END  
> irb(main):077:1" 2  
> irb(main):078:1" END  
> irb(main):079:1\> )  
> 1  
> 2  
> =\> nil
> 
> If correctness of usage depends on parameters order - is this a bug?

I’m actually suprised, that your usage works.

puts(‘1’, \<\<END)  
2  
END

That’s how I’d have written it.  
You can also use multiple

puts(\<\<FIRST, \<\<SECOND)  
1  
FIRST  
2  
SECOND

Or apply methods

puts(\<\<FIRST.upcase, \<\<SECOND.capitalize)  
first  
FIRST  
second  
SECOND

# (will print “FIRST” newline “Second”)

Btw, no need for the ‘q’ method, puts can handle multiple arguments 😉

Regards  
Stefan
