I am developing a kernel in Ruby that adheres to the IPython messaging protocol. It integrates nicely with different Ruby gems as will be shown later.
This gives us a very fancy web notebook interface for Ruby. It's a very good tool for programming presentations. It's basically an in-browser REPL loop, with some extra goodies.
Install IRuby with:
gem install iruby
Start the IRuby notebook with:
iruby notebook
$stdout
and $stderr
are redirected to the notebook
In [1]:
puts 'Hello, world!'
In [2]:
$stderr.puts 'Error!'
The last computed result is returned.
In [3]:
Math.sqrt(2)
Out[3]:
This works even for images.
In [4]:
File.open('lib/iruby/static/base/images/src/ruby.svg')
Out[4]:
In [5]:
File.open('lib/iruby/static/base/images/ipynblogo.png')
Out[5]:
In [6]:
IRuby.display '<b style="color:green">Hello, world!</b>', mime: 'text/html'
Out[6]:
In [7]:
IRuby.html '<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350></iframe>'
Out[7]:
$\LaTeX$ is rendered using MathJax.
In [8]:
IRuby.display IRuby.latex <<-'TEX'
\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}
TEX
IRuby.math('F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
Out[8]:
Out[8]:
Arrays and Hashes can be printed as HTML tables.
In [9]:
IRuby.display IRuby.table([1,2,[],3])
IRuby.display IRuby.table({a:1,b:2,c:3})
IRuby.display IRuby.table([[11,12,13,14],[21,22,23],'not an Array',[31,32,33,34]])
IRuby.display IRuby.table({a:[11,12,13,14],b:[21,22,23],c:[31,32,33,34]})
IRuby.display IRuby.table([{a:1,b:2,c:3},'not an Array',{a:2,b:3,c:4,e:5}])
IRuby.display IRuby.table([{a:1,b:2,c:3},{a:2,b:3,c:4,d:5},{0=>:x,1=>:y},[:a,:b,:c]])
Out[9]:
Out[9]:
Out[9]:
Out[9]:
Out[9]:
Out[9]:
Pry is an enhanced Ruby REPL. It will be automatically used by IRuby if available. You can use the code browsing utilities for example.
In [10]:
ls Array
In [11]:
require 'RMagick'
img = Magick::Image.new(240, 300)
gc = Magick::Draw.new
gc.fill('#FFAA00')
x = 120
y = 32.5
gc.polygon(x, y, x+29, y+86, x+109, y+86,
x+47, y+140, x+73, y+226, x, y+175,
x-73, y+226, x-47, y+140, x-109, y+86,
x-29, y+86)
gc.draw(img)
img
Out[11]:
In [12]:
require 'gnuplot'
Gnuplot::Plot.new do |plot|
plot.xrange '[-0.5:0.5]'
plot.title 'Example plot'
plot.ylabel 'x'
plot.xlabel 'sin(1/x)'
plot.samples 10000
plot.data << Gnuplot::DataSet.new('sin(1/x)') do |ds|
ds.with = 'lines'
ds.linewidth = 2
end
end
Out[12]:
You can also create nice 3D plots
In [13]:
Gnuplot::SPlot.new do |plot|
plot.title 'Spiral'
plot.nokey
plot.parametric
plot.hidden3d
plot.view '80,50'
plot.isosamples '60,15'
plot.xrange '[-8:8]'
plot.yrange '[-8:8]'
plot.zrange '[-8:8]'
plot.urange '[-2*pi:2*pi]'
plot.vrange '[-pi:pi]'
plot.data << Gnuplot::DataSet.new('cos(u)*(cos(v)+3), sin(u)*(cos(v)+3), sin(v)+u') do |ds|
ds.with = 'lines'
end
end
Out[13]:
In [14]:
require 'rubyvis'
Rubyvis::Panel.new do
width 150
height 150
bar do
data [1, 1.2, 1.7, 1.5, 0.7, 0.3]
width 20
height {|d| d * 80}
bottom(0)
left {index * 25}
end
end
Out[14]:
In [15]:
require 'gruff'
g = Gruff::Line.new
g.title = 'Gruff Example'
g.data :Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]
g.data :Charles, [80, 54, 67, 54, 68, 70, 90, 95]
g.data :Julie, [22, 29, 35, 38, 36, 40, 46, 57]
g.data :Jane, [95, 95, 95, 90, 85, 80, 88, 100]
g.data :Philip, [90, 34, 23, 12, 78, 89, 98, 88]
g.data :Arthur, [5, 10, 13, 11, 6, 16, 22, 32]
g
Out[15]:
In [16]:
require 'matrix'
Matrix[[1,2,3],[1,2,3]]
Out[16]: