$ uname -m -r -s -v
Darwin 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
$ ruby -v
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
$ cat ./here_documents.rb
testdata = 'foo bar'
# here document
# 識別子 EOS1 までがリテラル
eos1 = <<EOS1
hello world
#{testdata}
hello good-bye
EOS1
# here document
# 式展開
eos2 = <<"EOS2"
hello world
#{testdata}
hello good-bye
EOS2
# here document
# 式展開しない
eos3 = <<'EOS3'
hello world
#{testdata}
hello good-bye
EOS3
# indent
# 終端行をインデント
eos4 = <<-EOS4
hello world
#{testdata}
hello good-bye
EOS4
# indent
# 終端行をインデント
eos5 = <<-'EOS5'
hello world
#{testdata}
hello good-bye
EOS5
# here documents
# 複数のヒアドキュメント
eos6 = <<EOS6A, <<-EOS6B, <<'EOS6C'
hello world
#{testdata}
hello good-bye
EOS6A
hello world
#{testdata}
hello good-bye
EOS6B
hello world
#{testdata}
hello good-bye
EOS6C
puts 'eos1'
puts eos1
puts 'eos2'
puts eos2
puts 'eos3'
puts eos3
puts 'eos4'
puts eos4
puts 'eos5'
puts eos5
puts 'eos6'
puts eos6
$ ruby ./here_documents.rb
eos1
hello world
foo bar
hello good-bye
eos2
hello world
foo bar
hello good-bye
eos3
hello world
#{testdata}
hello good-bye
eos4
hello world
foo bar
hello good-bye
eos5
hello world
#{testdata}
hello good-bye
eos6
hello world
foo bar
hello good-bye
hello world
foo bar
hello good-bye
hello world
#{testdata}
hello good-bye
Ref. リテラル - ヒアドキュメント (行指向文字列リテラル)
tags: ruby
Posted by NI-Lab. (@nilab)