How to Drop to the Irb Prompt from a Running Script

How do I drop to the IRB prompt from a running script?

you can use ruby-debug to get access to irb

require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"

when program reaches debugger you will get access to irb.

Copy paste from irb excluding prompt at start of line?

You can hold the ⌥ Option key and drag for a rectangular selection:

You can also customize IRB's prompt. Here's an example of the built-in --noprompt option which provides a blank prompt:


how do I prevent a script loaded into irb from keeping previous values?

You're not restarting irb, so it depends on what AmazonProduct[] does. From its source:

def self.[](locale)
@requests[locale] ||= Request.new(locale)
end

It's caching, creating a new request iff one doesn't exist yet.

From one standpoint, it's "because it doesn't have a new". new is only called when the locale hasn't been loaded yet. From another, it's less new/not-new, but caching, without documenting it may do so.

Given the behavior, it's a reasonable assumption--and why my first thought was []'s implementation.


Regarding not restarting irb: if there isn't a mechanism to reload the cache (I didn't check), quickest thing to do would be to monkey-patch [] and always retrieve a new Request for a given locale.

Is it possible to configure the IRB prompt to change dynamically?

Here's a quick hack to get the working dir. It's sort of fragile, but it worked on ruby 1.8.7 and 1.9.2.

Set your prompt string to something like this:

"%N(%m):%03n:%i %~> ".tap {|s| def s.dup; gsub('%~', Dir.pwd); end }

The "%~" directive is not understood by irb itself, so I used it to do the replacement. This hack relies on the fact that irb calls dup to generate the prompt.

How to format irb command prompt

The irb man page has a section on "Customizing prompt". Here's mine for example:

IRB.conf[:PROMPT][:CUSTOM] = {
:PROMPT_I => ">> ",
:PROMPT_S => "%l>> ",
:PROMPT_C => ".. ",
:PROMPT_N => ".. ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :CUSTOM
IRB.conf[:AUTO_INDENT] = true

To use this, add it to your ~/.irbrc file (creating it if it doesn't exist.)

How Do You Clear The IRB Console?

On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

why does irb freak out sometimes when I paste in a script?

That's because of TAB characters you have in your source file. Indent with spaces. :-)

Is there a way to get the interactive ruby shell after running a script?

IRB is the interactive shell so irb -r _path_to_file_

irb --help
Usage: irb.rb [options] [programfile] [arguments]
-f Suppress read of ~/.irbrc
-m Bc mode (load mathn, fraction or matrix are available)
-d Set $DEBUG to true (same as `ruby -d')
-r load-module Same as `ruby -r'
-I path Specify $LOAD_PATH directory
-U Same as `ruby -U`
-E enc Same as `ruby -E`
-w Same as `ruby -w`
-W[level=2] Same as `ruby -W`
--inspect Use `inspect' for output (default except for bc mode)
--noinspect Don't use inspect for output
--readline Use Readline extension module
--noreadline Don't use Readline extension module
--prompt prompt-mode
--prompt-mode prompt-mode
Switch prompt mode. Pre-defined prompt modes are
`default', `simple', `xmp' and `inf-ruby'
--inf-ruby-mode Use prompt appropriate for inf-ruby-mode on emacs.
Suppresses --readline.
--simple-prompt Simple prompt mode
--noprompt No prompt mode
--tracer Display trace for each execution of commands.
--back-trace-limit n
Display backtrace top n and tail n. The default
value is 16.
--irb_debug n Set internal debug level to n (not for popular use)
-v, --version Print the version of irb

I dont know why yours didn't but here are all the options.



Related Topics



Leave a reply



Submit