OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
Thanks,
···
–
Bil Kleb
NASA Langley Research Center
Hampton, Virginia, USA
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
Thanks,
–
Bil Kleb
NASA Langley Research Center
Hampton, Virginia, USA
You’re not (necessarily) stupid. irb has no obvious code export
feature. And I can’t give you a real answer, but since it’s a
low-traffic time, I’ll try to point you in a helpful direction.
If you search the list for ‘irb’ and ‘history’, you should find
something in the last two months about how you can access irb’s
history programatically from within irb. If you can work out how to
do that (perhaps it’s even in the docs?, after all, it just uses
readline), then you can write a method that writes the entire history
to the named file.
Put that method in your ~/.irbrc file, and it will be accessible from
the irb prompt.
Cheers,
Gavin
On Sunday, March 30, 2003, 10:31:55 PM, Bil wrote:
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
Quoth Bil Kleb W.L.Kleb@larc.nasa.gov
On Sun, 30 Mar 2003 21:31:55 +0900:
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
I never thought about that before. You may know that Unix lets you
record any console interaction …
% script irb-session
Script started, file is irb-session
% irb
irb(main):001:0> puts ‘hello’
hello
=> nil
irb(main):002:0> exit
% exit
Script done, file is irb-session
%
… but input and output appear interleaved in the file, so it doesn’t
really give you just the code you wrote. Although you could run the
file through a filter that found code lines based on the prompt pattern,
even then I think it would still have your backspaces and such.
– Mark
“Bil Kleb” W.L.Kleb@larc.nasa.gov wrote in message
news:3E86E3B0.7060507@LaRC.NASA.Gov…
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?Thanks,
Bil Kleb
NASA Langley Research Center
Hampton, Virginia, USA
Looking quickly through the nutshell book it doesn’t give a way to save an
IRB session. I think the intention was not to write a whole program under
IRB but to just test snippets of code so you can see interactively what is
going on.
Done it. Put this in your ~/.irbrc
def dumphist(path)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line
end
end
end
All you needed to know was that irb uses “Readline” to do command-line
manipulation (including history), then look up Nutshell to see that
Readline::HISTORY is where it’s at. Doesn’t sound thread-safe.
Gavin
On Sunday, March 30, 2003, 10:31:55 PM, Bil wrote:
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
I do it the other way round: Typically I write the code in an editor and
then paste it into the IRB window.
robert
“Bil Kleb” W.L.Kleb@larc.nasa.gov schrieb im Newsbeitrag
news:3E86E3B0.7060507@LaRC.NASA.Gov…
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?Thanks,
Bil Kleb
NASA Langley Research Center
Hampton, Virginia, USA
OK, so I admit: I’m stupid. How do I save the code I’ve generated
during an irb session?
This is something that Pete McBreen brought up in his talk at RubyConf2001 as well. His point was that irb is such a good tool for learning that it would be nice to be able to save output so that a person can interactively “build” a program, piece by piece. It’s an interesting idea, and I’ve found myself wishing for it more than a couple of times.
Gavin responded:
If you search the list for ‘irb’ and ‘history’,
you should find
something in the last two months about how you
can access irb’s
history programatically from within irb. If you
can work out how to
do that (perhaps it’s even in the docs?, after
all, it just uses
readline), then you can write a method that
writes the entire history
to the named file.
This is a good idea, though I’d like to see a little more (maybe it’ll get tricky now). I’d rather not capture code that generates a syntax error. I might even like to see the sum total of a class or module definition generated as a single definition. For example, I often do this kind of thing when I’m playing with irb (oversimplification, of course):
–start–
class MyClass
end
x = Myclass.new
class MyClass
def initialize(something)
(…do something with “something”)
end
end
x = MyClass.new(“asdf”)
(…do some other stuff with my new MyClass until I realize I want one more method)
class MyClass
def one_more_method
puts “Hello World”
end
end
x.one_more_method
–end–
This is just a silly example, of course, but it sure would be nice to have the option of this outputting something like:
–start–
class MyClass
def initialize(something)
@something = something
end
def one_more_method
puts “Hello World”
end
end
x = MyClass.new
x = MyClass.new(“asdf”)
x.one_more_methd
–end–
I also recognize that there could be problems with output generated in such a way, but it would be a nice option. It would allow a programmer to iteratively construct classes and objects as if using a textual IDE.
To do this (I’ve never looked at the guts of irb), I guess it would be better to add a filter-like thing to the input chain vs. using the readline history. Maybe I’ll play with this today. Anyone know if something like this is already possible?
Thanks,
Chad
On Sunday, March 30, 2003, 10:31:55 PM, Bil > wrote:
Readline::HISTORY is an Object. Nutshell says it behaves like an
Array. But it doesn’t #to_s like an array of strings, nor does it
recognise #size.
Here are the methods it has in common with Array, excluding methods
inherited from Object:
[“push”, “length”, “each”, “shift”, “empty?”, “”, “delete_at”,
“pop”, “=”, “<<”, “member?”, “sort”, “collect”, “detect”, “max”,
“entries”, “reject”, “each_with_index”, “find”, “min”, “select”,
“include?”, “grep”, “map”, “find_all”]
Here are the Array methods that Readline::HISTORY doesn’t have:
[“clear”, “&”, “reverse”, “sort!”, “slice”, “<=>”, “uniq!”,
“delete_if”, “*”, “indexes”, “nitems”, " +", “rindex”, “last”,
“rassoc”, “filter”, “each_index”, “flatten”, “-”, “at”, “fill”,
“reverse!”, “collect!”, “slice!”, “|”, “compact”, “reject!”,
“indices”, “uniq”, “replace”, “reverse_each”, “to_ary”, “flatten!”,
“pack”, “index”, “join”, “concat”, “first”, “delete”, “assoc”, “size”,
“map!”, “unshift”, “compact!”]
Can anyone comment (speculation accepted) why the Readline class does
not simply use an Array for its HISTORY? If it’s a matter of
write-access, it would be more elegant to write a module
ReadOnlyArray, that redefines/removes the appropriate methods, so that
this module can be reused.
I’m looking at the source code, but since it’s in C I doubt it will
enlighten me much.
Cheers,
Gavin
On Sunday, March 30, 2003, 11:41:20 PM, Gavin wrote:
All you needed to know was that irb uses “Readline” to do command-line
manipulation (including history), then look up Nutshell to see that
Readline::HISTORY is where it’s at. Doesn’t sound thread-safe.
Wow. Thanks Gavin. If I may, I have modified your code just
a little:
def dumphist(path=“irb.log”)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line unless /^\s*dumphist/ =~ line
end
end
end
I’ve added a default log and I have excluded recording
of the dumphist command. Both of these are personal preference.
Thanks for the code!
On Sunday, 30 March 2003 at 22:41:20 +0900, Gavin Sinclair wrote:
Done it. Put this in your ~/.irbrc
Output irb history to named file
def dumphist(path)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line
end
end
end
Join in the new game that’s sweeping the country. It’s called
“Bureaucracy”. Everybody stands in a circle. The first person to do
anything loses.
I have not had the best of luck with that. I have not pinned down
the cause of the failures, but, if I have more than a few
lines, irb usually gets confused.
What’s your trick?
Output irb history to named file
def dumphist(path=“irb.log”)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line unless /^\s*dumphist/ =~ line
end
end
end
Is Readline a 1.8 thing?
D:\ruby\bin>more .irbrc
def dumphist(path=“irb.log”)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line unless /^\s*dumphist/ =~ line
end
end
end
D:\ruby\bin>irb
irb(main):001:0> puts VERSION
1.6.6
nil
irb(main):002:0> dumphist
NameError: uninitialized constant Readline
from ./.irbrc:4:in dumphist' from ./.irbrc:3:in
open’
from ./.irbrc:3:in `dumphist’
from (irb):2
Chris
http://clabs.org
I sometimes just ‘require’ the file I’m working on.
Gavin
On Monday, March 31, 2003, 11:46:08 PM, Jim wrote:
On Monday, 31 March 2003 at 22:34:12 +0900, Robert Klemme wrote:
I do it the other way round: Typically I write the code in an editor and
then paste it into the IRB window.robert
I have not had the best of luck with that. I have not pinned down
the cause of the failures, but, if I have more than a few
lines, irb usually gets confused.
What’s your trick?
“Jim Freeze” jim@freeze.org schrieb im Newsbeitrag
news:20030331085304.A50772@freeze.org…
I do it the other way round: Typically I write the code in an editor
and
then paste it into the IRB window.I have not had the best of luck with that. I have not pinned down
the cause of the failures, but, if I have more than a few
lines, irb usually gets confused.What’s your trick?
Maybe I always use onlye some lines with this method. For larger tests i
write a separate script anyway. Btw: I’m working on Win2k.
robert
On Monday, 31 March 2003 at 22:34:12 +0900, Robert Klemme wrote:
No, more likely a platform thing. What are you using? All the same,
I expect you can do
require ‘readline’
in IRB. Perhaps, for some reason, your IRB is not doing that itself,
which is what the dumphist code relies on.
Gavin
On Tuesday, April 1, 2003, 12:29:12 AM, Chris wrote:
Output irb history to named file
def dumphist(path=“irb.log”)
File.open(path, ‘w’) do |file|
Readline::HISTORY.each do |line|
file.puts line unless /^\s*dumphist/ =~ line
end
end
end
Is Readline a 1.8 thing?
No, more likely a platform thing. What are you using?
win2k. It can’t find ‘readline’ which I suspect is expected on Windows, IIRC
from other times this has come up.
Chris
Have nice & productive week everyone.
I am trying to create a CGI:Session , but I am having troubles
understanding the CGI behavior.
I am trying to use the CGI class just to handle session information, and
use a simple 'print’
to generate my html document,
After creating a CGI object it takes control of the STDOUT ?
#!/usr/bin/env ruby
#----------------->
#Abstraction of my problem
#This code does not work
def head(title)
require 'cgi’
require 'cgi/session’
print “Content-type: text/html\r\n\r\n”
#Session handling
cgi=CGI::new("html3")
session=CGI::Session(cgi)
print "You are logged in as: #{session[\"login\"]}" if
session[“login”]
html_out= "<HTML>" +
"<TITLE>#{title}</TITLE>" +
"<head>"
print html_out
end
#-----------------<
thks
-r.
rodrigo-
a couple of things, some of which may or may not apply to you - but all of
which i hacked through :
shebang ‘#!/usr/bin/env ruby’ may or may NOT work depending on whether or
not ruby is in user 'nobody’s (or whatever you web server runs as) path,
you may want to use ‘#!/path/to/ruby’ instead.
placing ‘cgi=CGI::new(“html3”)’ in a method could cause the cgi program to
hang. this method will attempt to read from stdin, and block if it
cannot. thus calling this > once (i’m assuming you may have other methods
other than ‘head(title)’) may cause your cgi to hang
CGI doesn’t ‘take over’ STDOUT, but does use it. you can use it too if
you like. i personally do not use the cgi methods too much, but do tend
to do something like
content = <<-html
#{stuff}
html
cgi.out { content }
when i’m not using amrita.
when composing output for cgi’s consider doing something like
html_out = ‘’
html_out << “” <<
“#{title}” <<
“”
instead of String.+() since ‘+’ creates a new object for each call.
that being said (sorry if that was more/less than you asked for), i can get
your code (modified) to run as :
http://eli.fsl.noaa.gov/tmp/tmp.cgi
hope that helps.
-a
ps.
unless you have serious speed contraints, i would highly reccomend looking at
amrita - it makes cgi programs ridulously short and easy to read.
On Tue, 1 Apr 2003, Bermejo, Rodrigo wrote:
Have nice & productive week everyone.
I am trying to create a CGI:Session , but I am having troubles
understanding the CGI behavior.I am trying to use the CGI class just to handle session information, and
use a simple ‘print’
to generate my html document,After creating a CGI object it takes control of the STDOUT ?
#!/usr/bin/env ruby
#----------------->
#Abstraction of my problem
#This code does not work
def head(title)
require ‘cgi’
require ‘cgi/session’
print “Content-type: text/html\r\n\r\n”#Session handling cgi=CGI::new("html3") session=CGI::Session(cgi) print "You are logged in as: #{session[\"login\"]}" if
session[“login”]
html_out= "<HTML>" + "<TITLE>#{title}</TITLE>" + "<head>" print html_out
end
#-----------------<thks
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
“Bermejo, Rodrigo” wrote:
I am trying to create a CGI:Session , but I am having troubles
understanding the CGI behavior.I am trying to use the CGI class just to handle session information, and
use a simple ‘print’
to generate my html document,After creating a CGI object it takes control of the STDOUT ?
#!/usr/bin/env ruby
#----------------->
#Abstraction of my problem
#This code does not work
def head(title)
require ‘cgi’
require ‘cgi/session’
print “Content-type: text/html\r\n\r\n”#Session handling cgi=CGI::new("html3") session=CGI::Session(cgi) print "You are logged in as: #{session[\"login\"]}" if
session[“login”]
html_out= "<HTML>" + "<TITLE>#{title}</TITLE>" + "<head>" print html_out
end
#-----------------<
I recently scratched a similar headache in mod_ruby w/ eruby. The CGI
object needs a chance to output some headers, which will include the
cookie-setting headers.
In my case, calling CGI#header worked nicely. (Instead of the whole
Content-type caboodle.)
HTH
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
- placing ‘cgi=CGI::new(“html3”)’ in a method could cause the cgi program to
hang. this method will attempt to read from stdin, and block if it
cannot. thus calling this > once (i’m assuming you may have other methods
other than ‘head(title)’) may cause your cgi to hang
Actually it does cache the query it found first time (by defining some
constants), and so if you create a second CGI object it will use the same
parameters without reading from stdin again. But it’s still not good style,
you’d be much better to create a single cgi object and then pass it to the
method(s) that need it. Or better, put it in an instance variable:
require ‘cgi’
class GUI
def initialize(cgi)
@cgi = cgi
end
def run
… real work goes here
end
end
cgi = CGI.new
app = GUI.new(cgi)
app.run
Now the methods in class GUI all have access to @cgi without having to pass
it around.
You can also make this work under fastCGI by replacing the last three lines
with:
require ‘fcgi’ # available from RAA
FCGI.each do |cgi|
app = GUI.new(cgi)
app.run
end
With a little more work you can get your errors displayed in the browser
too:
FCGI.each_cgi do |cgi|
begin
app = GUI.new(cgi)
app.run
rescue Exception => detail
puts cgi.header
print <<EOS
#{CGI.escapeHTML(detail.backtrace.join(\"\n\"))}" if detail.backtrace puts "" end end
when composing output for cgi’s consider doing something like
html_out = ‘’
html_out << “” <<
“#{title}” <<
“”instead of String.+() since ‘+’ creates a new object for each call.
Or use inline ‘here’ documents (see <<EOS in the example above)
Regards,
Brian.
On Tue, Apr 01, 2003 at 04:57:21AM +0900, ahoward wrote: