![](/style/images/good.png)
![](/style/images/bad.png)
Idiosyncratic Ruby: Big Data Without End
source link: https://idiosyncratic-ruby.com/59-big-data-without-end.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Big Data Without End
Ruby's big DATA
constant does more than you might expect!
Everything after the __END__
keyword (at the beginning of the line) is not interpreted as Ruby, but can be retrieved with the big¹ DATA
constant.² This is an example big-data.rb
script:
p DATA.read
__END__
big data
Big DATA
is a File object, which you can read
. The example will output "big data"
. An example of real-world usage is inline templating within the sinatra web framework.³
¹ Do not confuse with the small Data
class, which is a CRuby implementation detail
² Big DATA
is not defined, if you have no __END__
. Furthermore, it is not available if you did not execute the script directly, but loaded or required it.
³ Actually, not really
Is the Data Section Enough?
Wait a minute! Big DATA
is a File object? What file exactly?
p DATA.path
p DATA.lineno
__END__
big data
The output will be "big-data.rb"
and 3
. The big DATA
object points to the source file itself at a specific position! And look, we can alter this:
DATA.rewind
puts DATA.gets("\n__END__")[0..-9]
__END__
big data
It will now read the source code of itself:
DATA.rewind
puts DATA.gets("\n__END__")[0..-9]
Also See
More Idiosyncratic Ruby
Recommend
-
9
Ruby TRICKS of 2018 Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of P...
-
6
Nothing to Disable Ruby's mode of operation can be altered with some --enable-* / --disable-* command-line switches....
-
13
Super Snakes Have you ever been confused by the __underscores__ required by some of __RUBY__'s features? You can get i...
-
7
The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language. Even though the Unicode standard evolves consta...
-
10
Clear Case of Unclear Casing Recent Ruby versions allow you to choose from a wide-range of uppercase letters - beyond just ASCII - to st...
-
8
Nothing to Escape What is your wild guess: How many different ways does Ruby provide for inserting a NULL byte into a double-quote...
-
8
How does nothing (as in nil, null, or nan) compare to nothing? Equality Equality ==...
-
9
Assignments In-Style The introduction of pattern matching in Ruby 2.7 brought us a new style of multi-assigning local variables: The
-
6
Warning: The Experiment Ruby's Warning module learned some new tricks in...
-
5
Ruby comes with good support for Unicode-related features. Read on if you want to learn more about important Unicode fundamentals and how to use them in Ruby… …or just watch my talk from RubyConf 2017:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK