Meaning of __FILE__ = $0 as given on ruby-lang.org
Suppose your file is foo.rb, and defines a Foo class. The file can be used in one of two ways.
The first, which you're already familiar with, is to include it in another file or in IRB. It'll go something like:
# in otherfile.rb
require 'foo'
foo = Foo.new
The if __FILE__ == $0
line is for the second use case, where you make the file executable and call it directly from the shell. It'll go something like
# in the shell
./foo.rb
# alternatively: ruby foo.rb
if __FILE__ == $0 problem with main
Where is the file that defines Camping
or 'Menu
'? You will need to 'require
' those files:
require 'camping'
require 'menus' # etc
class Main
#..
Read more about require here.
What does __FILE__ == $PROGRAM_NAME mean in ruby?
__FILE__
always returns the path of the source file. It's not a variable so you can't assign value to it. Whether it returns a relative path or an absolute one depends on how you run the script.
$PROGRAM_NAME
or $0
by default returns the command that boots the program (minus the path of ruby interpreter). For example, you have a script file test.rb
like this:
#!/usr/bin/env ruby
puts __FILE__
puts $PROGRAM_NAME
If you run this script with ruby test.rb
, it prints
test.rb
test.rb
If you run the script with ruby /path/to/test.rb
, it prints
/path/to/test.rb
/path/to/test.rb
If you give the script an execution permission and run it with ./test.rb
, it prints
./test.rb
./test.rb
Unlike __FILE__
, $PROGRAM_NAME
and $0
are real global variables, and you can change their values. $PROGRAM_NAME
and $0
are aliases to each other, so you change the value of either one, the value of the other will change accordingly. For example, you have a test2.rb
like this:
#!/usr/bin/env ruby
$0 = 'Hello, world!'
puts $0
puts $PROGRAM_NAME
it prints
Hello, world!
Hello, world!
Ruby 'script = $0'
$0
is one of Ruby's global variables. From here:
$0 -- Contains the name of the script being executed. May be assignable.
Can I write Ruby code that is executed only when my script is run, but not when it is required?
This is usually done with
if __FILE__ == $0
Foo.run
end
but I prefer
if File.identical?(__FILE__, $0)
Foo.run
end
because programs like ruby-prof can make $0 not equal __FILE__
even when you use --replace-progname
.
$0
refers to the name of the program ($PROGRAM_NAME
), while __FILE__
is the name of the current source file.
Checking if a Ruby program was executed or imported via require
Change foo.rb to read:
if __FILE__ == $0
puts "Hello"
end
That checks __FILE__
- the name of the current ruby file - against $0
- the name of the script which is running.
Require file without executing code?
Is it possible to load a file without having it to run the code?
No, everything in a ruby file is executable code, including class and method definitions (you can see this when you try to define a method inside an if-statement for example, which works just fine). So if you wouldn't execute anything in the file, nothing would be defined.
You can however tell ruby that certain code shall only execute if the file is run directly - not if it is required. For this simply put the code in question inside an if __FILE__ == $0
block. So for your example, this would work:
file.rb
def method
puts "This won't be outputted."
end
if __FILE__ == $0
puts "This will not be outputted."
end
main.rb
require "./file"
Related Topics
Installing Ruby on MAC Os X 10.8.2
Ruby: How to Add "# Encoding: Utf-8" Automatically
Ruby on Rails - Drop Down Box on Change Event
Project Euler 1:Find the Sum of All the Multiples of 3 or 5 Below 1000
Errno::Econnrefused: Connection Refused - Connect(2) for Action Mailer
Is There a Built-In Binary-Search in Ruby
Rails Route to Username Instead of Id
How to Use Hash Keys as Methods on a Class
Yaml How Many Spaces Per Indent
Openssl::Ssl::Sslerror: Ssl_Connect Returned=1 Errno=0 State=Unknown State: Unknown Protocol
Using Custom To_JSON Method in Nested Objects
Do Ruby 'Require' Statements Go Inside or Outside the Class Definition
How to Generate Zip File Without Saving to the Disk with Ruby
How to Configure Capistrano to Use My Rvm Version of Ruby
How to Preserve Request Url With Nginx Proxy_Pass
Error Installing Nokogiri: Failed to Build Gem Native Extension & Libiconv Is Missing (Osx)