Class: Tryruby::Challenge

Inherits:
Object
  • Object
show all
Defined in:
lib/tryruby/challenge.rb

Overview

Single tutorial challenge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(help, test = nil, setup = nil, display_setup = false) ⇒ Challenge

Returns a new instance of Challenge.



8
9
10
# File 'lib/tryruby/challenge.rb', line 8

def initialize(help, test = nil, setup = nil, display_setup = false)
  @help, @test, @setup, @display_setup = help, test, setup, display_setup
end

Instance Attribute Details

#display_setupObject (readonly)

Returns the value of attribute display_setup.



6
7
8
# File 'lib/tryruby/challenge.rb', line 6

def display_setup
  @display_setup
end

#helpObject (readonly)

Returns the value of attribute help.



6
7
8
# File 'lib/tryruby/challenge.rb', line 6

def help
  @help
end

Instance Method Details

#setup(repl, vars) ⇒ Object



16
17
18
# File 'lib/tryruby/challenge.rb', line 16

def setup(repl, vars)
  repl.instance_exec(vars, &@setup) if @setup
end

#setup_sourceObject

Challenge setup as text



21
22
23
24
25
26
# File 'lib/tryruby/challenge.rb', line 21

def setup_source
  return '' unless @setup
  vars_name = @setup.parameters[0][1]
  source = @setup.to_source(strip_enclosure: true)
  source.gsub(/#{vars_name}\[:([^\]]+)\]/, '\1')
end

#test(repl, result, vars, output) ⇒ Object



12
13
14
# File 'lib/tryruby/challenge.rb', line 12

def test(repl, result, vars, output)
  repl.instance_exec(result, vars, output, &@test) if @test
end