Mark Hubbart
2/11/2005 11:46:00 PM
Hi,
On Sat, 12 Feb 2005 03:46:28 +0900, Harry Truax <htruax@stf.com> wrote:
> Hi,
>
> I noticed an error in one of my input lines -
>
> Here is another:
>
> box1_32 = $CUR % (condition(((box1_31).count).eq(0.to_s), '',
> condition((box1_31).gt(0.to_s), (box1_31) * ((7.9.to_s).percent), nil)))
> print 'box1_32', box1_32
>
> Convert the above to:
>
> box1_32 = $CUR % (box1_31.to_f > 0.0 ? box1_31.to_f * 0.079 : 0.0) print
> 'box1_32', box1_32
>
> THE INPUT LINE SHOULD BE:
>
> box1_32 = $CUR % (condition(((box1_31).count).eq(0.to_s), '',
> condition((box1_31).gt(0.to_s), (box1_31) * ((7.9.to_s).percent), nil)))
>
> THE OUTPUT SHOULD BE:
>
> box1_32 = $CUR % (box1_31.to_f > 0.0 ? box1_31.to_f * 0.079 : 0.0)
Perhaps it would be easier to modify Ruby to accept the original
syntax, than to modify the syntax to fit Ruby. For the above example:
def condition(cond, iftrue, iffalse)
cond ? iftrue: iffalse
end
class Object
alias_method :eq, :==
end
... and so on. Adding the proper methods should make it possible to
run your original code directly within Ruby.
If you don't want to pollute the namespace of your enclosing script,
you might try something like this (UNTESTED!)
funky_script_text = load_funky_script_file
ruby_conversion_code = <<EOC
def condition(cond, iftrue, iffalse)
#... more code
EOC
data_collection_code = <<EOC
data = {}
data[:return_values] = $Foo
data[:bar_stuff] = $Bar
data[:other] = $CUR
print Marshall::dump(data)
end
raw_data = IO.popen("ruby", "w+") do |ruby|
ruby.write ruby_conversion_code
ruby.write funky_script_text
ruby.write data_collection_code
ruby.close_write
ruby.read
end
data = Marshall::load(raw_data)
# do stuff with the reconstituted data hash
data[:other].each_line{|l| p l} #... and so on
HTH,
Mark