From: Wybo Dekker Date: 2012-04-24T17:39:45+09:00 Subject: Re: gem-packed script with DATA section does not work On 2012-04-23 22:57, Peter Zotov wrote: > Ryan Davis писал 23.04.2012 22:58: >> On Apr 23, 2012, at 04:49 , Wybo Dekker wrote: >> >>> That's clear, but does that mean there's no workaround? >>> Can Ruby not be told that the DATA section is somewhere else? >> >> This is my ((slightly?) horrible) workaround: >> >> data = File.read(__FILE__).split(/__END__/).last > > This is better: > data = File.read(__FILE__).split(/__END__/, 2).last That's great, thanks Peter, Ryan. But it needs a correction, because the script is now also split on the __END__ in the "data =..." line itself, so it needs a ^: data = File.read(__FILE__).split(/^__END__/, 2).last And Ryan's ,2 can be skipped if one wants more than one subsection in the DATA section. Here's my test script, including the idea of using it for erb templates: #!/usr/bin/env ruby require 'optparse' require 'erb' name = 'Unknown' OptionParser.new do |opt| opt.on('-n','--name=STRING',String, 'set the name' ) do |v| name = v end end.parse! data1,data2 = File.read(__FILE__). split(/^__END__/)[1..-1]. map { |x| ERB.new(x).result(binding) } puts '',"data1: #{data1}", '',"data2: #{data2}" __END__ Hello <%= name %> This is data 1, two lines. __END__ This is data 2, 3 lines, including this one. -- Wybo