Forum: Ruby using irb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-05 01:27
(Received via mailing list)
So far I've created a ~/bin/rb/dev/foo directory as a place to write and
test scripts. After reading the "pick axe" book some more it seems as is
irb is a better "place" or "way" to try out stuff. This correct?
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-01-05 02:24
(Received via mailing list)
John Maclean wrote:
> So far I've created a ~/bin/rb/dev/foo directory as a place to write
> and test scripts. After reading the "pick axe" book some more it
> seems as is irb is a better "place" or "way" to try out stuff. This
> correct?

Many (perhaps most) Rubyists use irb to try out simple Ruby stuff. I
suspect you would agree. In any case it's easy enough for you to give it
a try and make up your own mind.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-01-05 02:39
(Received via mailing list)
Timothy Hunter wrote:
> a try and make up your own mind.
I edit in gvim, with the Ruby menu and macro stuff,  and it's trivial to
edit something and press F5 to run the current file.

If I end up with something memorable, it's already in a file, easy to
rename, move, cay and paste, whatever.

If your preferred editor is not fast-loading, though, then irb may
better suit you.  I tend not to use it much.


James

--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
279e2d5cbf8130df3001ba885f58f5fb?d=identicon&s=25 Paul Legato (Guest)
on 2006-01-05 02:48
(Received via mailing list)
John Maclean wrote:
> it seems as is irb is a better "place" or "way" to try out stuff. This correct?

irb is great for rapidly getting a feel for how things work, since you
get instant feedback on each line. If something doesn't go as expected,
you can instantly start experimenting on the live state of ruby to
figure out what's going on.

That said, it is not necessarily the best tool for every job, and I
certainly wouldn't recommend using it alone. I often keep an irb running
in a shell window while I'm running a script in another shell, which I'm
editing in emacs. (Emacs has all kinds of integrated stuff for running
ruby directly that I haven't looked into yet, too.)

irb know-how also carries over directly into the Ruby debugger ("ruby -r
debug yourscript.rb"), another great way to figure out how things work.

Best,
Paul
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2006-01-05 04:07
(Received via mailing list)
On Thu, Jan 05, 2006 at 10:47:27AM +0900, Paul Legato wrote:
> certainly wouldn't recommend using it alone. I often keep an irb running
Assuming you mean that using irb alone shouldn't give you the impression
you all there is to know about Ruby, I agree.  If you mean that it
shouldn't be used alone for other purposes than getting a feel for the
language, I disagree: I've found that irb makes a better calculator
application (for my tastes) than any other simple calculator app I've
ever had the (dis)pleasure to encounter.  I use it for stuff like
calculating my income tax rate, for instance, and the ability to save
intermediate values in variables while calculating something complex is
great.

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

"A script is what you give the actors.  A program
is what you give the audience." - Larry Wall
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-01-05 04:28
(Received via mailing list)
Timothy Hunter wrote:
> a try and make up your own mind.
>

But beware. You can get strange results 1% of the time... as a result
of how irb works, not actual bugs.

If in any doubt, try the same thing outside irb.


Hal
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-01-05 07:35
(Received via mailing list)
On 1/4/06, James Britt <james_b@neurogami.com> wrote:

> > Many (perhaps most) Rubyists use irb to try out simple Ruby stuff. I
> > suspect you would agree. In any case it's easy enough for you to give it
> > a try and make up your own mind.
>
> I edit in gvim, with the Ruby menu and macro stuff,  and it's trivial to
> edit something and press F5 to run the current file.

I use plain old vim and gvim and have both irb and the ruby
interpreter bound to keyboard shortcuts.

This way, I can write out whole programs and run them, or just pop
open an irb session right inside my editor. :)

Combine this with 'ri what's under my cursor' and you've got a rockin'
development environment
A38c2fb3b5c09d5449d6fc37daf79d8d?d=identicon&s=25 NBarnes (Guest)
on 2006-01-05 10:32
(Received via mailing list)
After a great deal of annoyance getting started with Ruby, I'm getting
good results from the use of SciTE.  SciTE will output directly into
an editor frame at runtime for quick feedback, which I found very
useful in my early Ruby work and continue to enjoy later on.
A52b0e1c5d982f2512a03c5dbfd033d6?d=identicon&s=25 Dick Davies (Guest)
on 2006-01-05 11:44
(Received via mailing list)
On 05/01/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:

> I use plain old vim and gvim and have both irb and the ruby
> interpreter bound to keyboard shortcuts.
>
> This way, I can write out whole programs and run them, or just pop
> open an irb session right inside my editor. :)
>
> Combine this with 'ri what's under my cursor' and you've got a rockin'
> development environment

Nice, have you got any config snippets you wouldn't mind sharing?
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-01-05 19:57
(Received via mailing list)
On 1/5/06, Dick Davies <rasputnik@gmail.com> wrote:
>
> Nice, have you got any config snippets you wouldn't mind sharing?

I'll paste in my .vimrc and my .irbrc file (the latter was lovingly
lifted from JEG2)

.vimrc
---------------
" An example for a vimrc file.
"
set nobackup
set nocompatible
set backspace=indent,eol,start

set history=50		" keep 50 lines of command line history
set ruler		" show the cursor position all the time
set showcmd		" display incomplete commands
set incsearch		" do incremental searching

map Q gq

set autoindent		" always set autoindenting on
set shiftwidth=2
set tabstop=2
set expandtab
set tw=80
set nohlsearch
map <silent> <C-N> :se invhlsearch<CR>

filetype on
filetype indent on
filetype plugin on
syntax on
if !exists( "*EndToken" )
  function EndToken()
    let current_line = getline( '.' )
    let braces_at_end = '{\s*\(|\(,\|\s\|\w\)*|\s*\)\?$'
    if match( current_line, braces_at_end ) >= 0
      return '}'
    else
      return 'end'
    endif
  endfunction
endif

noremap <F5> :set invpaste paste?<Enter>
imap <F5> <C-O><F5>
set pastetoggle=<F5>

map <C-R> :!ruby %<CR>
map <C-A> :!rake<CR>
map <C-I> :!ri <cword><CR>

imap <C-L> <ESC>:execute 'normal o' . EndToken()<CR>O
imap <C-B> def test_<C-L><BS><BS><BS><BS><BS><BS>
"{{{ Ruby block delimiter conversion: do end <=> { }
"Copyright (c) 2005 Mauricio Fernandez
"Subject to same licensing terms as Ruby.
" requires matchit and friends
" since it uses the % and = bindings
function! s:String_Strip(str)
    let s = substitute(a:str, '\v^\s*', '', '')
    return substitute(s, '\v\s*$', '', '')
endfunction
function! s:RubyBlockBraceToDoEnd(lineno)
    " { } => do end
    let oldz = getreg("z")
    call setreg("z", "")
    execute 'normal ^f{%l"zd$'
    let suffix = s:String_Strip(getreg("z"))
    call setreg("z", oldz)
    let orig = getline(".")
    let repl = substitute(orig, '\v\s*\{\s*(\|[^|]*\|)?.*', ' do \1',
'')
    call setline(".", repl)
    let nextline = substitute(orig, '\v[^{]*\v\s*\{\s*(\|[^|]*\|)?', '',
'')
    let nextline = substitute(nextline, '\}[^}]*$', '', '')
    let numlines = 0


    " uncomment one of the following:

    " (1) just insert the body without splitting the lines on ;
    "call append(a:lineno, nextline)
    "call append(a:lineno+1, 'end' . suffix)
    "

    " (2) try to split on ; ...
    call append(a:lineno, 'end' . suffix)
    " this is what we would want to do:
    "let nextline = substitute(nextline, ';', "\n", 'g')

    while stridx(nextline, ";") != -1
	let eom = stridx(nextline, ";")
	let line = s:String_Strip(strpart(nextline, 0, eom))
	call append(a:lineno + numlines, line)
	let numlines = numlines + 1
	let nextline = strpart(nextline, eom+1, strlen(nextline) - eom - 1)
    endwhile
    let nextline = s:String_Strip(nextline)
    if strlen(nextline) > 0
	call append(a:lineno + numlines, nextline)
	let numlines = numlines + 1
    endif

    " this is what it all began with...
    "execute 'normal :s/\v\s*\{\s*(\|.*\|)?/ do \1\r/
'
    "execute 'normal g_cw
end'
    execute 'normal V' . (1 + numlines) . 'j='
    "echo "{ } => do end"
endfunction

function! s:RubyBlockDoEndToBrace(_firstline, _lastline)
    " do end => { }
    let linenum = a:_firstline + 1
    let orig = getline(".")
    while linenum < a:_lastline - 1
	let addline = getline(linenum)
	if '\v^\s*$' !~ addline
	    let addline = substitute(addline, '\v^\s*', '', '')
	    let addline = substitute(addline, '\s*$', '; ', '')
	    let orig = orig . addline
	endif
	let linenum = linenum + 1
    endwhile
    let l = substitute(getline(a:_lastline-1), '\v^\s*', '', '')
    let l = substitute(l, '\s*$', '', '')
    let orig = orig . l
    let l = substitute(getline(a:_lastline), '\v^\s*end(\.|\s|$)@=', '
}', '')
    let l = substitute(l, '\s*$', '', '')
    let orig = orig . l

    "echo orig
    "input(orig)
    let repl = substitute(orig, '\v\s*do\s*(\|[^|]*\|)?', '{\1 ', '')
    "execute 'normal d' . (a:_lastline - a:_firstline) . 'j'
    execute ':' . a:_firstline . ',' . a:_lastline . 'd'
    call append(a:_firstline - 1, repl)
    execute ':' . a:_firstline
    "echo "do end => { }"
endfunction

map <SID>xx <SID>xx
let s:sid = maparg("<SID>xx")
unmap <SID>xx
let s:sid = substitute(s:sid, 'xx', '', '')

function! <SID>RubyBlockSwitchDelimiters() range
    set nofoldenable
    if a:firstline == a:lastline
	let braceidx = match(getline("."), '{')
	let doidx = match(getline("."), '\<do\>')
	if braceidx != -1 && (doidx == -1 || braceidx < doidx)
	    call s:RubyBlockBraceToDoEnd(a:firstline)
	elseif doidx != -1
	    execute 'normal /\<do\>' . "\n" . 'V%:call ' .
			\ s:sid . 'RubyBlockSwitchDelimiters()' . "\n"
	else
	    echo "No block found"
	end
    else
	call s:RubyBlockDoEndToBrace(a:firstline, a:lastline)
    endif
    "execute 'normal V2k='
    "execute 'normal v5j'
endfunction

command! -range B <line1>,<line2>call <SID>RubyBlockSwitchDelimiters()
vmap <Leader>B :call <SID>RubyBlockSwitchDelimiters()<cr>

------
and .irbrc
------

require 'irb/completion'
ARGV.concat [ "--readline" ]

if $0 == "irb"
        IRB.conf[:PROMPT][:XMP][:RETURN] = "\# => %s\n"

        IRB.conf[:EVAL_HISTORY] = 1000
        IRB.conf[:SAVE_HISTORY] = 100

        HISTFILE = "~/.irb.hist"
        MAXHISTSIZE = 100

        begin
         if defined? Readline::HISTORY
          histfile = File::expand_path( HISTFILE )
          if File::exists?( histfile )
                lines = IO::readlines( histfile ).collect {|line|
line.chomp}
                puts "Read %d saved history commands from %s." %
                  [ lines.nitems, histfile ] if $DEBUG || $VERBOSE
                Readline::HISTORY.push( *lines )
          else
                puts "History file '%s' was empty or non-existant." %
                  histfile if $DEBUG || $VERBOSE
          end

          Kernel::at_exit {
                lines = Readline::HISTORY.to_a.reverse.uniq.reverse
                lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if
lines.nitems > MAXHISTSIZE
                $stderr.puts "Saving %d history lines to %s." %
                  [ lines.length, histfile ] if $VERBOSE || $DEBUG
                File::open( histfile,
File::WRONLY|File::CREAT|File::TRUNC ) {|ofh|
                  lines.each {|line| ofh.puts line }
                }
          }
        end
        end

        def ri(*names)
        system(%{ri #{names.map {|name| name.to_s}.join(" ")}})
        end
end
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-01-05 19:57
(Received via mailing list)
On 1/5/06, Dick Davies <rasputnik@gmail.com> wrote:
>
> Nice, have you got any config snippets you wouldn't mind sharing?

You probably just wanted this though: ;) (slap in .vimrc)

map <C-R> :!ruby %<CR>
map <C-A> :!rake<CR>
map <C-I> :!ri <cword><CR>
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2006-01-07 13:37
(Received via mailing list)
Why is this line necessary in .irbrc?

~ ryan ~
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-07 13:37
(Received via mailing list)
On Jan 5, 2006, at 11:29 AM, J. Ryan Sobol wrote:

> Why is this line necessary in .irbrc?

I was having trouble with IRb at some point and that was the fix.
I'm trying to remember now, but it's not coming to me.  I think it
was Rails related.  Console or Breakpointer maybe...

It was some time ago and may not be an issue now.

James Edward Gray II
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-07 13:37
(Received via mailing list)
On Thu, 05 Jan 2006 17:36:03 -0000, James Edward Gray II
<james@grayproductions.net> wrote:

> On Jan 5, 2006, at 11:29 AM, J. Ryan Sobol wrote:
>
>> Why is this line necessary in .irbrc?
>
> I was having trouble with IRb at some point and that was the fix.  I'm
> trying to remember now, but it's not coming to me.  I think it was Rails
> related.  Console or Breakpointer maybe...
>
> It was some time ago and may not be an issue now.
>

Still useful though - I use something similar to allow symlinks to
launch
different irb 'profiles'. I just have 'tirb' I use for testing stuff
(lots
of requires in .irbrc) and 'irb' that's pretty much standard.

Cheers,
This topic is locked and can not be replied to.