From: Robert Klemme Date: 2012-04-24T22:20:59+09:00 Subject: Re: gem-packed script with DATA section does not work On Tue, Apr 24, 2012 at 2:59 PM, Wybo Dekker wrote: > On 2012-04-24 11:08, Robert Klemme wrote: >> # single quotes are important! >> MY_DATA = <<'DAT' >> doc is here >> DAT >> >> And if you place it at the end of the script you have pretty much the >> same as with __END__ - plus, you can even define more documents that >> way.  You could also do > > Maybe I don't see the clue, but putting the doc at the end of the script > (and thus necessarily using it before its definition) results in: > > uninitialized constant MY_DATA (NameError) Putting it at the end does not necessarily mean you use it before the definition especially if the file is a library file (i.e. no code gets executed). $ ruby19 y.rb 3 $ cat -n y.rb 1 2 3 require './x' 4 5 puts size() 6 $ cat -n x.rb 1 2 3 def size 4 return MY_DATA.length 5 end 6 7 8 MY_DATA = "foo" 9 I cannot judge your case since your demo gem just contained demo content. If you insist on having the data at the end, there are other approaches as well: $ ruby19 -r ./z.rb -e 'p get' "This is\nmy\n\ndata\n" rklemme@padrklemme2 /tmp/tmpenv.sh-8oDIBS $ cat -n z.rb 1 2 3 def get 4 File.read(__FILE__)[%r{^=begi\s*#\s*DATA$\r?\n(.*)^=end\s*\z}m, 1] 5 end 6 7 =begin # DATA 8 This is 9 my 10 11 data 12 =end Kind regards rober -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/