Anyone know why __END__ and DATA don't work in Rails?

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

On Sep 11, 12:12 pm, Gavin [email protected] wrote:

Gavin

The DATA constant is only created for the running script. Thus,
require’d files do not get the DATA constant.

see the following:

[email protected]:~ $ cat data.rb
puts DATA.read
END
helloooooo
[email protected]:~ $ cat d2.rb
require ‘data’
[email protected]:~ $ ruby data.rb
helloooooo
[email protected]:~ $ ruby d2.rb
/home/xeno/data.rb:1:in <top (required)>': uninitialized constant DATA (NameError) from d2.rb:1:inrequire’
from d2.rb:1:in `’
[email protected]:~ $

That makes sense!

Thanks for that!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs