Created
September 14, 2011 22:03
-
-
Save anonymous/1217929 to your computer and use it in GitHub Desktop.
Kernel eval peculiarities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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