module Tryruby::Shell

Shell extension

Attributes

levels[W]

Public Instance Methods

before_loop() click to toggle source
Calls superclass method
# File lib/tryruby/shell.rb, line 11
def before_loop
  super
  @level, @challenge = 0, -1
  next_challenge
  setup_challenge if @challenge_changed
  @challenge_changed = false
  @output = ''
end
challenge() click to toggle source
# File lib/tryruby/shell.rb, line 66
def challenge
  level[@challenge] if level && @challenge >= 0
end
format_error(error) click to toggle source
Calls superclass method
# File lib/tryruby/shell.rb, line 50
def format_error(error)
  Colors.error(super(error))
end
format_result(result) click to toggle source
Calls superclass method
# File lib/tryruby/shell.rb, line 54
def format_result(result)
  Colors.result(super(result))
end
help_challenge() click to toggle source
# File lib/tryruby/shell.rb, line 92
def help_challenge
  puts challenge.help if challenge
end
level() click to toggle source
# File lib/tryruby/shell.rb, line 62
def level
  levels[@level] if @level >= 0
end
levels() click to toggle source
# File lib/tryruby/shell.rb, line 58
def levels
  @levels ||= []
end
loop_eval(str) click to toggle source
Calls superclass method Tryruby::NextFix#loop_eval
# File lib/tryruby/shell.rb, line 33
def loop_eval(str)
  stdout_saved = $stdout
  $stdout = StringIO.new
  begin
    super(str)
  ensure
    @output = $stdout.string
    $stdout = stdout_saved
    puts @output if @output != ''
  end
end
loop_once() click to toggle source
Calls superclass method
# File lib/tryruby/shell.rb, line 20
def loop_once
  throw :ripl_exit unless challenge
  super
  result = @error_raised ? @error : @result
  if challenge && \
    challenge.test(repl_self, result, read_variables, @output)
    puts Colors.success('Success!')
    next_challenge
  end
  setup_challenge if @challenge_changed
  @challenge_changed = false
end
next_challenge() click to toggle source
# File lib/tryruby/shell.rb, line 70
def next_challenge
  @challenge += 1
  while level && !challenge
    @challenge = 0
    @level += 1
  end
  @challenge_changed = true if challenge
  nil
end
prev_challenge() click to toggle source
# File lib/tryruby/shell.rb, line 80
def prev_challenge
  lvl, chall = @level, @challenge - 1
  while lvl > 0 && chall < 0
    lvl -= 1
    chall = levels[lvl].length - 1
  end
  return if chall < 0
  @level, @challenge = lvl, chall
  @challenge_changed = true
  nil
end
print_eval_error(error) click to toggle source
Calls superclass method

Private Instance Methods

read_variables() click to toggle source
# File lib/tryruby/shell.rb, line 102
def read_variables
  vars = eval('local_variables', @binding).map do |var|
    [var, @binding.local_variable_get(var)]
  end
  Hash[vars]
end
repl_self() click to toggle source
# File lib/tryruby/shell.rb, line 98
def repl_self
  eval('self', @binding)
end
setup_challenge() click to toggle source
# File lib/tryruby/shell.rb, line 113
def setup_challenge
  return unless challenge
  vars = read_variables
  challenge.setup(repl_self, vars)
  write_variables(vars)
  puts challenge.setup_source if challenge.display_setup
  help_challenge
end
write_variables(vars) click to toggle source
# File lib/tryruby/shell.rb, line 109
def write_variables(vars)
  vars.each { |sym, value| @binding.local_variable_set(sym, value) }
end