Ptkwt1
15 November 2002 02:41
1
Let’s say I’ve got some strings with Ruby code to be eval’ed, like:
evalstr = ‘puts “this is the string”’
and let’s say I want to get the part that’s sent to STDOUT (by puts) and
capture it in a string, like so:
outputStr = eval evalstr #=> outputStr = “this is the string”
I suspect I could do this by redefining ‘puts’, but is there a way of
doing it so that puts is only redefined within the context of the eval?
Like:
puts "this is the regular puts"
outputStr = eval ‘puts “this string gets returned from special puts”’
Oh, now that I typed all of this in , I suspect I could do something with
the binding argument that gets passed into eval as an optional 2nd
parameter…
Phil
Phil Tomson ptkwt@shell1.aracnet.com writes:
Let’s say I’ve got some strings with Ruby code to be eval’ed, like:
evalstr = ‘puts “this is the string”’
and let’s say I want to get the part that’s sent to STDOUT (by puts) and
capture it in a string, like so:
outputStr = eval evalstr #=> outputStr = “this is the string”
I suspect I could do this by redefining ‘puts’, but is there a way of
doing it so that puts is only redefined within the context of the eval?
Ruby 1.7 has StringIO.
require ‘stringio’
def redirect
orig_defout = $defout
$defout = StringIO.new
yield
$defout.string
ensure
$defout = orig_defout
end
evalstr = ‘puts “this is the string”’
outputStr = redirect { eval evalstr }
p outputStr
If you want to use StringIO in Ruby 1.6, check Ruby Shim:
http://www.ruby-lang.org/en/raa-list.rhtml?name=Ruby+Shim+for+1.6
or MoonWolf’s StringIO:
//http://www.moonwolf.com/ruby/archive/stringio01.zip
···
–
eban
Ptkwt1
15 November 2002 08:02
3
In article 41-Fri15Nov2002124848+0900-eban@os.rim.or.jp ,
···
WATANABE Hirofumi eban@os.rim.or.jp wrote:
Phil Tomson ptkwt@shell1.aracnet.com writes:
Let’s say I’ve got some strings with Ruby code to be eval’ed, like:
evalstr = ‘puts “this is the string”’
and let’s say I want to get the part that’s sent to STDOUT (by puts) and
capture it in a string, like so:
outputStr = eval evalstr #=> outputStr = “this is the string”
I suspect I could do this by redefining ‘puts’, but is there a way of
doing it so that puts is only redefined within the context of the eval?
Ruby 1.7 has StringIO.
require ‘stringio’
def redirect
orig_defout = $defout
$defout = StringIO.new
yield
$defout.string
ensure
$defout = orig_defout
end
evalstr = ‘puts “this is the string”’
outputStr = redirect { eval evalstr }
p outputStr
I have ruby 1.7 from CVS but the require ‘stringio’ failed.
Where is 1.7’s stringio?
Phil
The problem with this approach is that it’s not thread safe($defout is
tampered), and that the same can be done with much less coding using
instance_eval method:
···
On éåí ùéùé, 15 áðåáîáø 2002, 05:48, WATANABE Hirofumi wrote:
Ruby 1.7 has StringIO.
require ‘stringio’
def redirect
orig_defout = $defout
$defout = StringIO.new
yield
$defout.string
ensure
$defout = orig_defout
end
evalstr = ‘puts “this is the string”’
outputStr = redirect { eval evalstr }
p outputStr
===
require ‘stringio’
s=StringIO.new
evalstr =‘puts “this is the string”’
s.instance_eval evalstr
p s
Phil Tomson ptkwt@shell1.aracnet.com writes:
I have ruby 1.7 from CVS but the require ‘stringio’ failed.
Where is 1.7’s stringio?
% ls ext
CVS Setup.nt curses extmk.rb pty stringio win32ole
Setup Setup.x68 dbm fcntl racc strscan
Setup.atheos Win32API digest gdbm readline syslog
Setup.dj aix_mksym.rb dl iconv sdbm tcltklib
Setup.emx configsub.rb etc nkf socket tk
% ls ext/stringio
CVS MANIFEST README depend stringio.c
ChangeLog says:
···
Mon Mar 11 21:03:37 2002 Nobuyoshi Nakada nobu.nakada@nifty.ne.jp
* ext/stringio: new.
–
eban