When writing Ruby you have the option to end the file early with __END__. Anything after the __END__ is assigned to a constant called DATA which is available within the file. I recently tried using this in Rails and it didn''t work. Instead I get an uninitialized constant exception. Anyone know why? Has it been disabled in Rails? thanks Gavin
pharrington
2009-Sep-11 18:33 UTC
Re: Anyone know why __END__ and DATA don''t work in Rails?
On Sep 11, 12:12 pm, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote:> When writing Ruby you have the option to end the file early with > __END__. Anything after the __END__ is assigned to a constant called > DATA which is available within the file. > > I recently tried using this in Rails and it didn''t work. Instead I get > an uninitialized constant exception. Anyone know why? > > Has it been disabled in Rails? > > thanks > > GavinThe DATA constant is only created for the running script. Thus, require''d files do not get the DATA constant. see the following: xeno@amrita:~ $ cat data.rb puts DATA.read __END__ helloooooo xeno@amrita:~ $ cat d2.rb require ''data'' xeno@amrita:~ $ ruby data.rb helloooooo xeno@amrita:~ $ ruby d2.rb /home/xeno/data.rb:1:in `<top (required)>'': uninitialized constant DATA (NameError) from d2.rb:1:in `require'' from d2.rb:1:in `<main>'' xeno@amrita:~ $
That makes sense! Thanks for that! On Sep 11, 7:33 pm, pharrington <xenogene...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 11, 12:12 pm, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote: > > > When writing Ruby you have the option to end the file early with > > __END__. Anything after the __END__ is assigned to a constant called > > DATA which is available within the file. > > > I recently tried using this in Rails and it didn''t work. Instead I get > > an uninitialized constant exception. Anyone know why? > > > Has it been disabled in Rails? > > > thanks > > > Gavin > > The DATA constant is only created for the running script. Thus, > require''d files do not get the DATA constant. > > see the following: > > xeno@amrita:~ $ cat data.rb > puts DATA.read > __END__ > helloooooo > xeno@amrita:~ $ cat d2.rb > require ''data'' > xeno@amrita:~ $ ruby data.rb > helloooooo > xeno@amrita:~ $ ruby d2.rb > /home/xeno/data.rb:1:in `<top (required)>'': uninitialized constant > DATA (NameError) > from d2.rb:1:in `require'' > from d2.rb:1:in `<main>'' > xeno@amrita:~ $