Skip to content

Instantly share code, notes, and snippets.

@ixti
Last active August 29, 2015 14:02
Show Gist options
  • Save ixti/21b291cdb80574bf29a9 to your computer and use it in GitHub Desktop.
Save ixti/21b291cdb80574bf29a9 to your computer and use it in GitHub Desktop.
Ruby data inliner
module Kernel
def __DATA__
s, m = "", false
::File.open caller.first.split(/:\d+:in/).first, "r" do |io|
io.each_line do |line|
next s << line if m
m = line =~ /^__END__$/
end
end
fail NameError, "uninitialized constant __DATA__" unless m
s
end
end

Usage

require "yaml"
require "__data__"

class Status
  REASONS = YAML.load __DATA__
end

__END__

200: OK
404: Not Found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment