How would I have irb require the config/boot.rb file from the directory I run irb in?
I would do:
config_file = File.join(Dir.pwd, 'config', 'boot.rb')
require config_file if File.exist?(config_file)
Why isn't current directory on my Ruby path?
In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative
. My apps tend to look like this:
require 'some_gem'
require 'another_gem'
require_relative 'lib/init'
And then lib/init.rb
can have:
require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb
It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.
Edit: Unfortunately (for reasons I don't know and haven't looked into) require_relative
doesn't work specifically in irb. For this you can:
- do what you initially described: either
$: << '.'
or$:.unshift '.'
, or you can use
load 'myfile.rb'
orrequire './myfile'
instead:irb(main):001:0> Dir['*.rb']
=> ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]
irb(main):002:0> require 'a'
LoadError: no such file to load -- a
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):2
from /usr/local/bin/irb:12:in `<main>'
irb(main):003:0> require_relative 'a'
LoadError: cannot infer basepath
from (irb):3:in `require_relative'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'
irb(main):004:0> load 'a.rb'
a
=> true
irb(main):005:0> require './a'
a
=> true
Strange irb behaviour, listing content current directory
The following line will have tab characters in it:
raise StandardError
irb
uses readline
which means that the tab key is used for tab completion. Double tab will show you all the available options.
To see this in action, just launch irb
and hit the tab key twice.
See https://superuser.com/questions/37148/how-to-disable-double-tab-to-show-available-commands-in-linux-console for guidance on how to disable it.
Can you 'require' ruby file in irb session, automatically, on every command?
I usually create a simple function like this:
def reload
load 'myscript.rb'
# Load any other necessary files here ...
end
With that, a simple reload
will re-import all of the scripts that I'm working on. It's not automatic, but it's the closest thing that I've been able to come up with.
You may be able to override method_missing
to call this function automatically when your object is invoked with a method that doesn't exist. I've never tried it myself, though, so I can't give any specific advice. It also wouldn't help if you're calling a method that already exists but has simply been modified.
In my own laziness, I've gone as far as mapping one of the programmable buttons on my mouse to the key sequence "reload<enter>". When I'm using irb
, all it takes is the twitch of a pinky finger to reload everything. Consequently when I'm not using irb
, I end up with the string "reload" inserted in documents unintentionally (but that's a different problem entirely).
Rspec Ruby Basic Example Error
This is a great simple example to get started with rspec. To make everything work do the following:
- Place your bowling.rb file in
lib/bowling.rb. - Place your bowling_spec.rb file in
spec/bowling_spec.rb. - Run the command
rspec
if you are using rpsec 2.
spec/bowling_spec.rb - Run the command
spec spec/bowling_spec.rb
if you are using rspec 1.
Also, an updated example can be found here.
How to load a ruby file in to IRB?
From the require
docs:
Any constants or globals within the loaded source file will be available in the calling program’s global namespace. However, local variables will not be propagated to the loading environment.
So if in options.rb
you have something like:
key = something
(i.e. key
is a local in the file) then it will not be available in irb. If you make it a global (e.g. $key = 'something'
) or a constant (e.g. KEY = 'something'
) it should be available.
Related Topics
Ruby on Rails: What Reporting And/Or Charting Tools Are Available
Rails Development Server Is Slow and Takes a Long Time to Load a Simple Page
How to Make Rails 3 Assets Precompile Faster
Rails: Ensure Only One Boolean Field Is Set to True at a Time
How to Specify Post Params in a Rails Test
Ruby Method with Maximum Number of Parameters
Ruby: What Does the Snippet: (Num & 1) == 0 Exactly Do
Ruby: How to Chain Multiple Method Calls Together with "Send"
Are There Unintended Consequences of Ruby's 'Begin ... End' Without 'Rescue' Used as a Code Block
Mongodb and Mongoid in Production
Creating an Empty File in Ruby: "Touch" Equivalent
Error When Starting Rails Server: Warning: Insecure World Writable Dir /Usr in Path, Mode 040777
Accessing Ruby Class Variables with Class_Eval and Instance_Eval
Namespacing Thor Commands in a Standalone Ruby Executable
Cucumber Not Showing Coloured Output in Windows
How to Use Define_Method Inside Initialize()
What Are the Things You Would Like Improved in the Ruby Language