Module: Tryruby::Shell
- Includes:
- NextFix
- Defined in:
- lib/tryruby/shell.rb
Overview
Shell extension
Instance Attribute Summary collapse
Instance Method Summary collapse
- #before_loop ⇒ Object
- #challenge ⇒ Object
- #format_error(error) ⇒ Object
- #format_result(result) ⇒ Object
- #help_challenge ⇒ Object
- #level ⇒ Object
- #loop_eval(str) ⇒ Object
- #loop_once ⇒ Object
- #next_challenge ⇒ Object
- #prev_challenge ⇒ Object
- #print_eval_error(error) ⇒ Object
Instance Attribute Details
#levels ⇒ Object
58 59 60 |
# File 'lib/tryruby/shell.rb', line 58 def levels @levels ||= [] end |
Instance Method Details
#before_loop ⇒ Object
11 12 13 14 15 16 17 18 |
# 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 ⇒ Object
66 67 68 |
# File 'lib/tryruby/shell.rb', line 66 def challenge level[@challenge] if level && @challenge >= 0 end |
#format_error(error) ⇒ Object
50 51 52 |
# File 'lib/tryruby/shell.rb', line 50 def format_error(error) Colors.error(super(error)) end |
#format_result(result) ⇒ Object
54 55 56 |
# File 'lib/tryruby/shell.rb', line 54 def format_result(result) Colors.result(super(result)) end |
#help_challenge ⇒ Object
92 93 94 |
# File 'lib/tryruby/shell.rb', line 92 def help_challenge puts challenge.help if challenge end |
#level ⇒ Object
62 63 64 |
# File 'lib/tryruby/shell.rb', line 62 def level levels[@level] if @level >= 0 end |
#loop_eval(str) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 ⇒ Object
70 71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# 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) ⇒ Object
45 46 47 48 |
# File 'lib/tryruby/shell.rb', line 45 def print_eval_error(error) @error = error super(error) end |