Require ruby file without .rb extension?
Use load
instead:
load 'file-name'
Require ruby file without .rb extension?
Use load
instead:
load 'file-name'
Ruby 'require' and File Suffixes
A file that require
loads has to have an extension that is explicitly stated in the argument of require
, or otherwise, be either .rb
or .so
.
Solution for you is to use load
instead.
How does load/require/require_relative handle a file with no file extension?
From the documentation:
Ruby tries adding “.rb”, “.so”, and so on to the name until found.
http://apidock.com/ruby/Kernel/require
Execute ruby script without .rb extension?
In linux, the information about the interpreter is usually taken from the shebang line, not from the extension. That's why you basically don't need the extension (but usually need the execute bit in the file attributes).
I don't know what are the traditions in Ruby about file naming (is it considered a good thing or not to include an extension), but it's generally a good idea to follow it (whatever it is).
Require a ruby file in a ruby file
If config.rb is in your load path, you can require it at the top of your basics.rb file with require 'config'
. If it is not in your load path, you'll need something like require '/path/to/your/config'
.
The code you've posted will require the file. But only when someone POSTs to '/'.
Also, it's normal to omit the .rb extension when requiring ruby files. But you can include it if you like.
You can view your load path by inspecting the global variable $LOAD_PATH
. From the command line ruby -e 'puts $LOAD_PATH'
will print it for your version of ruby. You can also add directories to your load path.
Clean way to require all files in non-Rails Ruby app?
Just putting it out there: more code does not mean "dirty", just as often as less code does not mean "clean."
Requiring your files manually will give you less headache in the long run—it solves load order issues and prevents you from requiring files you don't actually want. Plus, anyone looking at your code later on has a nice, clean manifest of what's being loaded in your app.
My suggestions:
- Drop the
src
folder, it's redundant—your Ruby app is all source. - Map your namespaces to folders
- Use explicit requires (this becomes easier when you use #2:
Example:
game/
bin/
gamerunner
Gemfile
lib/
game.rb
game/
game_object.rb
models/
character.rb
spec/
spec_helper.rb
models/
character_spec.rb
You are writing specs / tests, aren't you?
Then, in lib/game.rb
:
require 'game/game_object.rb'
# require the rest of your library as you build it
module Game
end
And in your init:
require 'game'
require 'models/character.rb'
Much cleaner, much easier to extract from later on and should solve your problem.
How can I require code from another .rb file like in PHP?
Yes, this is possible, although certainly not something I'd recommend doing. This works:
includer.rb
:
puts var
include.rb
:
var = "Hello!"
eval(File.read("include.rb"), binding)
Running this (Ruby 2.2.1, Ruby 1.9.3) will print Hello!
. It works simply: eval
takes an optional binding with which to evaluate the code it is passed, and Kernel#binding
returns the current binding.
How do I tell RubyMine the file type when the file has no extension?
There are several ways to tell RubyMine that a file is of a particular type:
Begin the command file with a hashbang line that RubyMine recognizes as being Ruby. I don't know what exactly it recognizes, but all of the scripts that gems put in to my bin dir begin with
#!/usr/bin/env ruby
and when I make a new script which begins with that line RubyMine also recognizes it as being Ruby (even before I open it). I did notice that when I had a file without this line and then added it I had to quit and restart RubyMine for it to take effect.
Just open the file in RubyMine. When I open a file that RubyMine doesn't recognize, it asks me what type it is and remembers what I tell it.
Register the file manually:
- Go to Preferences -> IDE Settings -> File Types
- Under File Types, select "Ruby files"
- Under Registered Patterns, add a new pattern that matches
gem_name
It would be painful to have to do this for a lot of files, but it might be useful in some cases.
I'm surprised that method 2 didn't work for you already. Maybe you told RubyMine that the file was a text file. Check the Text file type in the File types preferences to be sure your file isn't registered as text.
Related Topics
Garbage Collector in Ruby 2.2 Provokes Unexpected Cow
Why Won't Ruby Allow Me to Specify Self as a Receiver Inside a Private Method
How to Specify a Struct as the Return Value of a Function in Rubyffi
Rails: Rake Db:Create:All (Could Not Connect to Server)
Cannot Install Any Ruby Gems on MAC Os - Ssl_Connect Error
Ruby 'Require' Call Fails on Custom Code
How to Parse Xml Nodes to CSV with Ruby and Nokogiri
Ruby Minitest Assert_Output Syntax
Ruby Defining Operator Procedure
Prawn PDF: I Need to Generate Nested Tables
Rails Activeadmin Drop Down Menu on New and Edit Forms
How to Run Perl and Ruby Scripts as Tasks in Ant