Class: Heyruby::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/heyruby/console.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



6
7
8
9
10
11
12
13
# File 'lib/heyruby/console.rb', line 6

def initialize
  Pry.config.should_load_rc = false
  Pry.config.prompt = [method(:normal_prompt), method(:wait_prompt)]
  Pry.hooks.add_hook :after_read, :prompt_for_hypothesis do |line, pry|
    prompt_for_hypothesis line, pry
  end
  @pry = Pry
end

Class Method Details

.startObject



46
47
48
# File 'lib/heyruby/console.rb', line 46

def self.start
  new.start
end

Instance Method Details

#normal_prompt(obj, nest_level, pry) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/heyruby/console.rb', line 15

def normal_prompt obj, nest_level, pry
  if @hypothesis
    @hypothesis = nil
    "Were you right?\nTry somthing else> "
  else
    "Hey Ruby‽> "
  end
end

#prompt_for_hypothesis(line, pry) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/heyruby/console.rb', line 28

def prompt_for_hypothesis line, pry
  print %Q<What do you think the result of
```
#{CodeRay.scan(line, :ruby).term}
```
will be? (Type . on a line by itself when done)
| >
  @hypothesis = []
  while (str = readline.chomp!) != "."
    print "| "
    @hypothesis << str
  end
end

#startObject



42
43
44
# File 'lib/heyruby/console.rb', line 42

def start
  @pry.start
end

#wait_prompt(obj, nest_level, pry) ⇒ Object



24
25
26
# File 'lib/heyruby/console.rb', line 24

def wait_prompt obj, nest_level, pry
  "Hey Ruby‽* "
end