irb idea: return value history 
Author Message
 irb idea: return value history

Here's a made-up irb session I wish I could have:

  irb(main):001:0> 17+4
  => 21
  irb(main):002:0> 'hello, fellow'
  => "hello, fellow"
  irb(main):003:0> history[1]
  => 21
  irb(main):004:0> x = !!
  => 21
  irb(main):005:0> x
  => 21
  irb(main):005:0> history
  [<what should go here? nil?>, 21, "hello, fellow", 21, 21, 21]

Perhaps something like this functionality already exists?  I don't know.  In
any case, it would be wonderful if I could pull back the return values from
previous lines.  It would also be nice if I could pull back the last return
value with something shorter than `history.last'.

In fact, `history[num]' is maybe not the best way of doing it; too many
characters.  maybe something like `/his' or even `/h'?  You would never
start a fresh line of Ruby with a slash, so maybe irb could rewrite those
for us.  Then we could have `/!' for last value returned.

Actually, history is probably misleading, since I'm interested in the return
value, not the input line... so perhaps something like `/12' would be
translated by irb to `$return_values[12]'.  So...

/num      -->  $return_values[num]
// or /!  -->  $return_values.last
/*        -->  $return_values.each {|val| puts val.inspect}
               # not inspect if flag is set?

So would this help anyone else out, too?  I find myself trying something
out, then *depending on what it does* I might want to do something else with
that value.  I end up preceding everything with `a = ' or something.

Lines which don't complete an expression could just be considered as
returning `nil'.

(Next step:  multi-line editing for multi-line expressions!  Then we can
ignore what I just wrote, because every "line" would return an expression.)

Chris



Wed, 27 Jul 2005 20:09:30 GMT  
 irb idea: return value history
Quote:
----- Original Message -----
[snip]
/num      -->  $return_values[num]
// or /!  -->  $return_values.last
/*        -->  $return_values.each {|val| puts val.inspect}
               # not inspect if flag is set?
----------------------------

On second thought, you would also want to be able to use the old return
values in other expressions, and not necessarily always at the beginning of
the line (though I probably do that most frequently).  So maybe a shorter
name than `$return_values' would be best.

  irb(main):022:0> RV[12] + RV[21]
  => "hello world"

Maybe a constant would be better?  `RV' for "returned values"?  But for the
record, I still like the slash tricks from above (/12, //, /*, others?).

Chris



Wed, 27 Jul 2005 20:20:43 GMT  
 irb idea: return value history

Quote:
> /num      -->  $return_values[num]
> // or /!  -->  $return_values.last
> /*        -->  $return_values.each {|val| puts val.inspect}
>                # not inspect if flag is set?

I'd like something like this too.

Quote:
> So would this help anyone else out, too?  I find myself trying something
> out, then *depending on what it does* I might want to do something else with
> that value.  I end up preceding everything with `a = ' or something.

Me too.  I usually hit the up-arrow key and continuously edit the line.

--
Daniel Carrera
Graduate Teaching Assistant.  Math Dept.
University of Maryland.  (301) 405-5137



Thu, 28 Jul 2005 03:25:19 GMT  
 irb idea: return value history
Hi --

Quote:

> In fact, `history[num]' is maybe not the best way of doing it; too many
> characters.  maybe something like `/his' or even `/h'?  You would never
> start a fresh line of Ruby with a slash, so maybe irb could rewrite those
> for us.  Then we could have `/!' for last value returned.

I start lines with / a lot, when testing regexes.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Thu, 28 Jul 2005 03:37:58 GMT  
 irb idea: return value history
Hi,

At Sat, 8 Feb 2003 21:09:30 +0900,

Quote:

> Here's a made-up irb session I wish I could have:

Try conf.eval_history.

irb(main):005:0> conf.eval_history=100
=> 100
irb(main):006:0> conf.eval_history
=> 100
irb(main):007:0> __
=> 5 100
6 100
irb(main):008:0> 1+2
=> 3
irb(main):009:0> _
=> 3
irb(main):010:0> __
=> 5 100
6 100
7 ...self-history...
8 3
9 3
irb(main):011:0> __[2]
=> nil
irb(main):012:0> a = "abc"
=> "abc"
irb(main):013:0> a.sub(/a/,"z")
=> "zbc"
irb(main):014:0> __
=> 5 100
6 100
7 ...self-history...
8 3
9 3
10 ...self-history...
11 nil
12 "abc"
13 "zbc"

--
Nobu Nakada



Thu, 28 Jul 2005 04:43:15 GMT  
 irb idea: return value history
To David:

  Of course!  I forgot regexes.  D'oh!

To Nobu:

  Beautiful!  Is there a way to set conf.eval_history automatically every
time?

Chris



Thu, 28 Jul 2005 09:04:39 GMT  
 irb idea: return value history

Quote:

> Is there a way to set conf.eval_history automatically every time?

Put it in your .irbrc file.

Gavin



Thu, 28 Jul 2005 17:21:37 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. history of "ignoring return value"

2. RETURN / RETURN-FROM and values

3. Missing file irb/irb.rb

4. irb ruby-script w/o exiting irb?

5. TKCon Idea - remove redundant history items

6. I Have been unable to get return data from Winsock.GetData method in LabView, Any Ideas

7. returning values through functions calls

8. ActiveX Double return value passed by reference?

9. Q:Pattern for nil return values

10. Smalltalk return value customs?0

11. return multiple values from an awk function?

12. Return value from awk snipet via korn shell subroutine

 

 
Powered by phpBB® Forum Software