Skip to content

Instantly share code, notes, and snippets.

Created September 14, 2011 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1217929 to your computer and use it in GitHub Desktop.
Save anonymous/1217929 to your computer and use it in GitHub Desktop.
Kernel eval peculiarities
bind = binding
eval 'require "rubyvis"', bind
vis = eval 'test.rb', bind, __FILE__, __LINE__
svg_s = vis.to_svg # a string
File.open("tmp.svg", "w") { |f| f.puts svg_s } # output for debugging only
svg_h = RSVG::Handle.new_from_data(svg_s).tap { |s| s.close }
# now render svg_h with GTK/Shoes...
# Incorrect STDERR output, incorrect plot, no errors.
require 'rubyvis'
dat = pv.range(0, 10, 0.1).map {|x|
o = Hash.new({:x=> x, :y=> Math.sin(x) + 2+rand()})
STDERR.puts o.inspect
o
}
STDERR.puts "size of dat: #{dat.size}"
STDERR.puts "first x = #{dat.first[:x]}"
STDERR.puts "first y = #{dat.first[:y]}"
#dat.each do |d|
# STDERR.puts [d.x,d.y].join("\t")
#end
#p data
w = 400
h = 200
x = pv.Scale.linear(dat, lambda {|d| d[:x]}).range(0, w)
y = pv.Scale.linear(dat, lambda {|d| d[:y]}).range(0, h)
STDERR.puts "after scale"
#/* The root panel. */
vis = pv.Panel.new()
.width(w)
.height(h)
.bottom(20)
.left(20)
.right(10)
.top(5)
STDERR.puts "a"
vis.add(pv.Line).
data(dat).
line_width(5).
left(lambda {|d| x.scale(d[:x])}).
bottom(lambda {|d| y.scale(d[:y])}).
anchor("bottom").add(pv.Line).
stroke_style('red').
line_width(1)
STDERR.puts "b"
vis
# No errors. Works fine, output is correct.
require 'rubyvis'
dat = pv.range(0, 10, 0.1).map {|x|
o = {:x=> x, :y=> Math.sin(x) + 2+rand()}
STDERR.puts o.inspect
o
}
STDERR.puts "size of dat: #{dat.size}"
STDERR.puts "first x = #{dat.first[:x]}"
STDERR.puts "first y = #{dat.first[:y]}"
#dat.each do |d|
# STDERR.puts [d.x,d.y].join("\t")
#end
#p data
w = 400
h = 200
x = pv.Scale.linear(dat, lambda {|d| d[:x]}).range(0, w)
y = pv.Scale.linear(dat, lambda {|d| d[:y]}).range(0, h)
STDERR.puts "after scale"
#/* The root panel. */
vis = pv.Panel.new()
.width(w)
.height(h)
.bottom(20)
.left(20)
.right(10)
.top(5)
STDERR.puts "a"
vis.add(pv.Line).
data(dat).
line_width(5).
left(lambda {|d| x.scale(d[:x])}).
bottom(lambda {|d| y.scale(d[:y])}).
anchor("bottom").add(pv.Line).
stroke_style('red').
line_width(1)
STDERR.puts "b"
vis
# Incorrect STDERR output and incorrect plot, no errors.
require 'rubyvis'
require 'ostruct'
dat = pv.range(0, 10, 0.1).map {|x|
o = OpenStruct.new(:x=> x, :y=> Math.sin(x) + 2+rand())
STDERR.puts o.inspect
o
}
STDERR.puts "size of dat: #{dat.size}"
STDERR.puts "first x = #{dat.first.x}"
STDERR.puts "first y = #{dat.first.y}"
#dat.each do |d|
# STDERR.puts [d.x,d.y].join("\t")
#end
#p data
w = 400
h = 200
x = pv.Scale.linear(dat, lambda {|d| d.x}).range(0, w)
y = pv.Scale.linear(dat, lambda {|d| d.y}).range(0, h)
STDERR.puts "after scale"
#/* The root panel. */
vis = pv.Panel.new()
.width(w)
.height(h)
.bottom(20)
.left(20)
.right(10)
.top(5)
STDERR.puts "a"
vis.add(pv.Line).
data(dat).
line_width(5).
left(lambda {|d| x.scale(d.x)}).
bottom(lambda {|d| y.scale(d.y)}).
anchor("bottom").add(pv.Line).
stroke_style('red').
line_width(1)
STDERR.puts "b"
vis
# Original test.rb. Incorrect plot.
require "rubyvis"
data = pv.range(0, 10, 0.1).map {|x|
OpenStruct.new({:x=> x, :y=> Math.sin(x) + 2+rand()})
}
#p data
w = 400
h = 200
x = pv.Scale.linear(data, lambda {|d| d.x}).range(0, w)
y = pv.Scale.linear(data, lambda {|d| d.y}).range(0, h)
#/* The root panel. */
vis = pv.Panel.new().
width(w).
height(h).
bottom(20).
left(20).
right(10).
top(5)
vis.add(pv.Line).
data(data).
line_width(5).
left(lambda {|d| x.scale(d.x)}).
bottom(lambda {|d| y.scale(d.y)}).
anchor("bottom").add(pv.Line).
stroke_style('red').
line_width(1)
vis
#
# Next two lines are uncommented when running without continuous evaluation.
# My app calls these after getting the Rubyvis::Panel (vis) back from eval.
#
# vis.render()
# vis.to_svg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment