Module: IRuby

Defined in:
lib/iruby/utils.rb,
lib/iruby/kernel.rb,
lib/iruby/backend.rb,
lib/iruby/command.rb,
lib/iruby/display.rb,
lib/iruby/ostream.rb,
lib/iruby/session.rb,
lib/iruby/version.rb

Defined Under Namespace

Classes: Command, Display, Kernel, MimeString, OStream, PlainBackend, PryBackend, Session

Constant Summary collapse

VERSION =
'0.1.9'

Class Method Summary collapse

Class Method Details

.display(obj, options = {}) ⇒ Object



15
16
17
# File 'lib/iruby/utils.rb', line 15

def self.display(obj, options={})
  Kernel.instance.display(obj, options)
end

.html(s) ⇒ Object



68
69
70
# File 'lib/iruby/utils.rb', line 68

def self.html(s)
  MimeString.new('text/html', s)
end

.latex(s) ⇒ Object



60
61
62
# File 'lib/iruby/utils.rb', line 60

def self.latex(s)
  MimeString.new('text/latex', s)
end

.math(s) ⇒ Object



64
65
66
# File 'lib/iruby/utils.rb', line 64

def self.math(s)
  MimeString.new('text/latex', "$$#{s}$$")
end

.table(obj) ⇒ Object



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
50
51
52
53
54
55
56
57
58
# File 'lib/iruby/utils.rb', line 19

def self.table(obj)
  return obj unless Enumerable === obj
  keys = nil
  size = 0
  rows = []
  obj.each do |row|
    row = row.flatten(1) if obj.respond_to?(:keys)
    if row.respond_to?(:keys)
      # Array of Hashes
      keys ||= Set.new
      keys.merge(row.keys)
    elsif row.respond_to?(:map)
      # Array of Arrays
      size = row.size if size < row.size
    end
    rows << row
  end
  table = '<table>'
  if keys
    keys.merge(0...size)
    table << '<tr>' << keys.map {|k| "<th>#{k}</th>"}.join << '</tr>'
  else
    keys = 0...size
  end
  rows.each do |row|
    table << '<tr>'
    if row.respond_to?(:map)
      row = keys.map {|k| "<td>#{row[k] rescue nil}</td>" }
      if row.empty?
        table << "<td#{keys.size > 1 ? " colspan='#{keys.size}'" : ''}></td>"
      else
        table << row.join
      end
    else
      table << "<td#{keys.size > 1 ? " colspan='#{keys.size}'" : ''}>#{row}</td>"
    end
    table << '</tr>'
  end
  html(table << '</table>')
end