I'd like to extend the default console application that is built as standard with bundle gem by applying some of the IRB config options.
Looking at the documentation, I can see that it should be possible for instance to change the prompt, and this works fine on an interactive session. For example I can play with the displayed prompt like this:
2.1.4 :001 > conf.prompt_mode=:SIMPLE
=> :SIMPLE
>>
?> conf.prompt_mode=:DEFAULT
=> :DEFAULT
irb(main):004:0>
However, I cannot find how to translate this into syntax for use in the console app. For example this script:
require 'irb'
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.start
Just starts with the generic configured prompt:
2.1.4 :001 >
I have spent some time trying to find an example use of IRB for a custom repl without loading global defaults, but not found anything I can copy from.
I can see that the undocumented method IRB.setup has something to do with this, it is setting all the config somehow. Is my only option to write my own version of IRB.start that applies my desired config after calling IRB.setup, or is there support for what I want to do built-in but not documented in standard location?
E.g. the following works, but I feel it's a bit heavy handed extending IRB module this way (and also prone to failing if IRB internals change).
require 'irb'
def IRB.custom_start custom_conf = {}
STDOUT.sync = true
IRB.setup(nil)
custom_conf.each do |k,v|
IRB.conf[k] = v
end
if #CONF[:SCRIPT]
irb = IRB::Irb.new(nil, #CONF[:SCRIPT])
else
irb = IRB::Irb.new
end
#CONF[:IRB_RC].call(irb.context) if #CONF[:IRB_RC]
#CONF[:MAIN_CONTEXT] = irb.context
trap("SIGINT") do
irb.signal_handle
end
begin
catch(:IRB_EXIT) do
irb.eval_input
end
ensure
irb_at_exit
end
end
IRB.custom_start :PROMPT_MODE => :SIMPLE
You can apply custom configurations in two ways.
The first one is to use irbrc file. It may be tricky in building console application (calling IRB.start from the ruby file instead of irb from the console).
The second one is the approach that you have described in the post. You can write your own IRB::start method based on the original one. There are exactly the same potential issues as in using undocumented API - it can break in the future with newer versions of irb.
You should think if you really need to build a console application on the top of irb. For example you can solve this problem using Pry. It allows to define configuration before starting interactive session.
require 'irb'
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.start
The approach above doesn't work because conf[:PROMPT_MODE] gets over-riden in a method called IRB.init_config here
When IRB.start is called, it calls IRB.setup which in turn calls the method IRB.init_config -- which over-rides conf[:PROMPT_MODE] setting.
Here is one approach which solves the problem (relies on internal knowledge of the implementation).
require 'irb'
module IRB
singleton_class.send(:alias_method, :old_setup, :setup)
def IRB.setup(ap_path)
IRB.old_setup(ap_path)
conf[:PROMPT_MODE] = :SIMPLE
end
end
IRB.start
While practicing Ruby, I decided it would be nice to have some sort of state machine gem to help me manage the application state of a basic Ruby app (not using Rails).
I didn't find one, or didn't know where to look. So I wrote one.
The gem's name is gk-application, it's on rubygems and the GitHub repo is here: https://github.com/gregkrsak/gk-application
Basically, an application is an instance of GK::Application, which can then be in one of four states: :stopped, :starting, :running or :stopping.
The application's code lives in the event handlers attached to each state. To start the app, set its state attribute to :starting.
If you have the gem installed and would prefer to have a project template built for you, simply use one of the following methods:
Using ruby:
ruby -e 'require "gk-application"' -e 'GK::Application.new.project'
Using irb:
$ irb
irb(main):001:0> require 'gk-application'
=> true
irb(main):002:0> GK::Application.new.project
=> nil
irb(main):003:0> quit
Either of which will generate a file named my_app.rb in the current directory, containing:
#!/usr/bin/env ruby
require 'gk-application'
my_app = GK::Application.new
my_app.on_starting = Proc.new do
puts 'Starting.'
my_app.state = :running
end
my_app.on_running = Proc.new do
puts 'Running.'
my_app.state = :stopping
end
my_app.on_stopping = Proc.new do
puts 'Stopping.'
my_app.state = :stopped
end
my_app.on_stopped = Proc.new do
puts 'Stopped.'
end
my_app.state = :starting
Which should get you started. Thanks for reading! Feel free to contribute to the code.
just having troubles to make I18n to work without Rails environment:
irb> require 'i18n'
=> true
irb> I18n.load_path=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml']
=> ["/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml"]
irb> I18n.load_path+=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/sk.yml']
=> ["/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml", "/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/sk.yml"]
irb> I18n.locale=:sk
=> :sk
irb> I18n.default_locale=:sk
=> :sk
irb> I18n.l Time.now
I18n::MissingTranslationData: translation missing:
sk.time.formats.default
from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:289:in
`handle_exception'
from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:159:in
`translate'
from
/usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n/backend/base.rb:55:in
`localize'
from /usr/lib/ruby/gems/1.9.1/gems/i18n-0.6.1/lib/i18n.rb:236:in
`localize'
from (irb):11
from /usr/bin/irb:12:in `<main>'
irb>
What am I doing wrong ? The sk.yml DOES contain sk.time.formats.default
element !!
In addition what's the I18n's default load_path(s) so I won't be
bothered to supply full paths to every translation YAML/Ruby file ?
Thanks.
You already set the search path for the language definitions with I18n.load_path.
It seems, this is enough when using rails. Without rails, you must also load the language definitions with I18n.backend.load_translations.
In summary, you need two steps:
I18n.load_path = Dir['*.yml']
I18n.backend.load_translations
The dictionaries are defined with language key, e.g. like:
en:
hello: "Hello world"
If you prefer to define your en.yml without language key, you may load them via
I18n.backend.store_translations(:en , YAML.load(File.read('en.yml')))
(You may also use a here-document or direct a ruby-hash).
It seems like your load_path is not being set correctly.
Try including the whole directory and if it's successful, you should see your :sk and :en files by calling I18n.load_path.
I18n.load_path = Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/*yml']
Setting the files paths directly can be a bit confusing since I18n won't raise an error if the file doesn't exist.
As a side note, I'd advise against including translations from the rails-i18n gem as the path may be different from one machine to another with different ruby versions etc.. a file local to the project would be better.
You'll need to install rails-i18n gem just to get localization data.
With this gem install, one can e.g. print month names in sk localization with:
require 'rails-i18n'
I18n.load_path += $LOADED_FEATURES
.select {|f| "rails-i18n.rb".in? f }
.collect {|f| f.sub('lib/rails-i18n.rb', 'rails/locale/sk.yml') }
I18n.locale = :sk
puts I18n.t('date.month_names').compact
This yields:
Január
Február
Marec
Apríl
Máj
Jún
Júl
August
September
Október
November
December
I have a ruby script that is failing due to my environment, I think it is demonstrated by this strange behaviour in irb ( I am also using rvm but don't think that is the problem)
>> ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.8.0]
>> irb
>> FileUtils.mkdir_p('tmp')
NameError: uninitialized constant FileUtils
from (irb):1
>> help
=> nil
>> FileUtils.mkdir_p('tmp')
=> "tmp"
The FileUtils command initially fails but then after typing Help (which also fails) it seems to work.
I have tried require 'rubygems' and require 'FileUtils' - which does fix the problem - but would like to understand whats happening here.
I didn't know there was a "help" command, but apparently it has dependency on FileUtils, probably to load help files. "help" is loading its requirements into the IRB session.
>> before = ObjectSpace.each_object.map { |i| i.class }.uniq
=> [Regexp, String, Array, Class, Hash, Module, Proc, MatchData, File, Binding, NoMemoryError, Float, SystemStackError, fatal, Bignum, Object, IO, Thread, ThreadGroup, IRB::Locale, IRB::Notifier::LeveledNotifier, IRB::Notifier::CompositeNotifier, IRB::StdioOutputMethod, IRB::Notifier::NoMsgNotifier, Enumerable::Enumerator, RubyToken::TkNL, RubyToken::TkEND, RubyToken::TkBITOR, RubyToken::TkIDENTIFIER, RubyToken::TkDOT, RubyToken::TkRBRACE, RubyToken::TkSPACE, RubyToken::TkfLBRACE, RubyToken::TkCONSTANT, RubyToken::TkASSIGN, IRB::SLex::Node, IRB::SLex, RubyLex, IRB::ReadlineInputMethod, IRB::WorkSpace, IRB::Context, IRB::Irb]
>> help
=> nil
>> after = ObjectSpace.each_object.map { |i| i.class }.uniq
=> [Regexp, String, MatchData, Array, Class, RI::ClassEntry, RI::MethodEntry, Hash, Module, Dir, Proc, File, Binding, NoMemoryError, Float, SystemStackError, fatal, Bignum, Object, IO, Thread, ThreadGroup, IRB::Locale, Range, IRB::Notifier::LeveledNotifier, IRB::Notifier::CompositeNotifier, IRB::StdioOutputMethod, IRB::Notifier::NoMsgNotifier, YAML::Syck::Resolver, Gem::ConfigFile, RubyToken::TkNL, RubyToken::TkIDENTIFIER, IRB::SLex::Node, IRB::SLex, RubyLex, IRB::ReadlineInputMethod, IRB::WorkSpace, IRB::Context, IRB::Irb, RI::TopLevelEntry, RI::RiReader, GetoptLong, RI::RiCache, RI::Options, RiDriver, Rational, Date::Infinity, Enumerable::Enumerator, RubyToken::TkRBRACE, DefaultDisplay, RI::TextFormatter]
>> after == before
=> false
>> after - before
=> [RI::ClassEntry, RI::MethodEntry, Dir, Range, YAML::Syck::Resolver, Gem::ConfigFile, RI::TopLevelEntry, RI::RiReader, GetoptLong, RI::RiCache, RI::Options, RiDriver, Rational, Date::Infinity, DefaultDisplay, RI::TextFormatter]
It loads the classes in after - before. Where is FileUtils you say? I think its a module that is part of Dir, but I am not 100% on that.
You need to require 'fileutils':
require 'fileutils'
FileUtils.pwd # => "/"
It is not included by the interpreter by default, which is why IRB doesn't preload it. Because IRB is interactive, it has to do some things on-the-fly that the interpreter will not, such as load help files. That it does so in response to your request isn't anything unexpected to me, it's just what it was programmed to do. I'm sure if you looked at its code you'd be able to trace it easily enough.
So, basically, all you are seeing is IRB respond correctly to your syntax error, then do what it was told to do in response to your "help" command.
If you absolutely have to know what it's doing, you can figure it out by asking IRB to trace its processing:
echo help | irb -f --trace > irb.out
will generate a tracing of what IRB does when "help" is entered. Searching through the file shows:
#0:/Users/greg/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/rdoc/ri/store.rb:2::-: require 'fileutils'
being required by store.rb as IRB loads 'ri'.
FileUtils is part of the Ruby standard library, so it is bundled with the interpreter, but not included automatically when the interpreter starts, like Dir and File. It is completely standalone, not a part of Dir.
In Python, you can do
>>> import sys
>>> sys.executable
'/usr/bin/python'
to get at the executable's location. Can you do the same thing just using something built-in to Ruby? It can be a special variable, method, etc.
If there isn't, what is the cleanest, most reliable way of determining the ruby executable's location in a cross-platform way?
Related:
How to get the python.exe location programmatically?
Run this in IRB:
require 'rbconfig'
key_length = RbConfig::CONFIG.keys.max{ |a,b| a.length <=> b.length }.length
RbConfig::CONFIG.keys.sort_by{ |a| a.downcase }.each { |k| puts "%*s => %s" % [key_length, k, RbConfig::CONFIG[k]] }
It will output an "awesome print" style list of all the Ruby configuration info.
ALLOCA =>
AR => ar
arch => x86_64-darwin10.5.0
ARCH_FLAG =>
archdir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.5.0
ARCHFILE =>
AS => as
ASFLAGS =>
BASERUBY => ruby
bindir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin
bindir is the path to the currently running Ruby interpreter. Above it in the list is BASERUBY => ruby.
RbConfig::CONFIG.values_at('bindir', 'BASERUBY').join('/')
=> "/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
Checking my work:
greg-mbp-wireless:~ greg$ which ruby
/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
There's a lot more information than this so it's worth running the code I added above to see what's available.
Linux-based systems are OK with
`whereis ruby`.split(" ")[1]
It will call whereis ruby and parse its' output for the second entry (first contains 'whereis:')
The more strict method is to call
puts `ls -al /proc/#{$$}/exe`.split(" ")[-1]
It will get the executable name for the current process (there is $$ variable and Process.pid method to obtain that) from /proc/pid/exe symlink information.
Looks like the only truly reliable way is
system("#{Gem.ruby} another_file.rb")
This works even for odd cases like jruby being run as a jar, etc.
Also see
OS.ruby_bin
https://github.com/rdp/os
It looks like the answer is in RbConfig::CONFIG
I think RbConfig::CONFIG['bindir'] provides the directory where the executable is located, the rest is s (or should be) straight forward.
RbConfig::CONFIG['bindir']+'/ruby' should work, even in windows as the exe can be ommitted
Works in a script, not from irb:
puts open($PROGRAM_NAME).readline.gsub /#! *([^ ]+).*/, '\1'
;-)