public
anonymous / eval code
Created

Kernel eval peculiarities

  • Download Gist
eval code
1 2 3 4 5 6 7
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...
test.explicit_hash.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 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
test.implicit_hash.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 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
test.openstruct.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# 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
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# 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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.