Class: TryRuby::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/try_ruby/repl.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ REPL

Returns a new instance of REPL.



3
4
5
# File 'lib/try_ruby/repl.rb', line 3

def initialize(options={})
  @options = options
end

Instance Method Details

#display(message) ⇒ Object



47
48
49
# File 'lib/try_ruby/repl.rb', line 47

def display(message)
  puts message
end

#process(command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/try_ruby/repl.rb', line 33

def process(command)
  begin
    result = eval(command)

    display "=> #{result}"

    display(script.next.formatted) if script.continue?
  rescue SyntaxError
    display "Oops, seems to have been some error. Care to try again?"
  rescue NameError
    display "Oops, you tried to use a method or variable that doesn't exist. Care to try again?"
  end
end

#scriptObject



7
8
9
# File 'lib/try_ruby/repl.rb', line 7

def script
  @script ||= Script.new(File.expand_path("../../../scripts/default.json", __FILE__))
end

#startObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/try_ruby/repl.rb', line 11

def start
  display "Hello! Interactive ruby ready."
  display script.intro.formatted

  while command = Readline.readline("> ", true)
    case command
    when "help"
      display "Soon, I will have help for you."
    when "next", "continue"
      display script.next.formatted
    when "prev", "previous", "back"
      display script.previous.formatted
    when "exit", "quit"
      break
    else
      process(command)
    end
  end

  display "Goodbye, I hope you had fun!\n"
end