Решение на Четвърта задача от Любослава Димитрова

Обратно към всички решения

Към профила на Любослава Димитрова

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 4 успешни тест(а)
  • 7 неуспешни тест(а)

Код

module UI
class TextScreen
def self.draw (&block)
result = Label.new
result.instance_eval(&block)
result.array.join("") + "\n"
end
end
class Label
def initialize
@array = []
end
attr_accessor :array
def label(text:, border: nil, style: nil)
stylish = lambda {return "#{border}#{text.send(style)}#{border}"}
not_stylish = lambda {return "#{border}#{text}#{border}"}
self.array << not_stylish.call if style == nil
self.array << stylish.call if style != nil
end
def vertical(border: nil, style: nil, &block)
helper = Label.new
helper.instance_eval(&block)
if border != nil then helper.array.bordered(border) end
if style != nil then helper.array.stylize(style) end
self.array << helper.array.map {|element| element + "\n"}
end
def horizontal(border: nil, style: nil, &block)
helper = Labels.new
helper.instance_eval(&block)
if border != nil then helper.array.bordered(border) end
if style != nil then helper.array.stylize(style) end
self.array << helper.array
end
end
end
class Array
def longest_word
group_by(&:size).max.last
end
def bordered(sign)
max = self.longest_word
self.map {|w| w = "#{sign}#{w.append(max.size - w.size)}#{sign}"}
end
def stylize(style)
self.map {|word| word.send(style)}
end
end
class String
def append(number)
self + " " * number
end
end

Лог от изпълнението

.FFFFF.F.F.

Failures:

  1) Command Line Toolkit adding horizontal group does not change the behavior
     Failure/Error: horizontal do
     NameError:
       uninitialized constant UI::Label::Labels
     # /tmp/d20141126-26053-4xtayh/solution.rb:35:in `horizontal'
     # /tmp/d20141126-26053-4xtayh/spec.rb:31:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:30:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Command Line Toolkit verical group orders elements vertically
     Failure/Error: expect do
       expected #<Proc:0xba202294@/tmp/d20141126-26053-4xtayh/spec.rb:42> to render as "      1\n      2\n      3\n"
     # /tmp/d20141126-26053-4xtayh/spec.rb:42:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Command Line Toolkit handles complex group nestings
     Failure/Error: horizontal do
     NameError:
       uninitialized constant UI::Label::Labels
     # /tmp/d20141126-26053-4xtayh/solution.rb:35:in `horizontal'
     # /tmp/d20141126-26053-4xtayh/spec.rb:58:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:28:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:28:in `vertical'
     # /tmp/d20141126-26053-4xtayh/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:56:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: vertical border: '|' do
     ArgumentError:
       negative argument
     # /tmp/d20141126-26053-4xtayh/solution.rb:62:in `*'
     # /tmp/d20141126-26053-4xtayh/solution.rb:62:in `append'
     # /tmp/d20141126-26053-4xtayh/solution.rb:52:in `block in bordered'
     # /tmp/d20141126-26053-4xtayh/solution.rb:52:in `map'
     # /tmp/d20141126-26053-4xtayh/solution.rb:52:in `bordered'
     # /tmp/d20141126-26053-4xtayh/solution.rb:29:in `vertical'
     # /tmp/d20141126-26053-4xtayh/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:85:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: horizontal border: '|' do
     NameError:
       uninitialized constant UI::Label::Labels
     # /tmp/d20141126-26053-4xtayh/solution.rb:35:in `horizontal'
     # /tmp/d20141126-26053-4xtayh/spec.rb:101:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:28:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:28:in `vertical'
     # /tmp/d20141126-26053-4xtayh/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:99:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Command Line Toolkit propagates upcase to child components
     Failure/Error: horizontal do
     NameError:
       uninitialized constant UI::Label::Labels
     # /tmp/d20141126-26053-4xtayh/solution.rb:35:in `horizontal'
     # /tmp/d20141126-26053-4xtayh/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:137:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Command Line Toolkit propagates downcase to child components
     Failure/Error: horizontal do
     NameError:
       uninitialized constant UI::Label::Labels
     # /tmp/d20141126-26053-4xtayh/solution.rb:35:in `horizontal'
     # /tmp/d20141126-26053-4xtayh/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-4xtayh/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-4xtayh/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-4xtayh/spec.rb:172:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02884 seconds
11 examples, 7 failures

Failed examples:

rspec /tmp/d20141126-26053-4xtayh/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-4xtayh/spec.rb:41 # Command Line Toolkit verical group orders elements vertically
rspec /tmp/d20141126-26053-4xtayh/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-4xtayh/spec.rb:84 # Command Line Toolkit wraps vertically-aligned components correctly in border
rspec /tmp/d20141126-26053-4xtayh/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-4xtayh/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-4xtayh/spec.rb:171 # Command Line Toolkit propagates downcase to child components

История (1 версия и 0 коментара)

Любослава обнови решението на 26.11.2014 15:56 (преди над 9 години)

+module UI
+
+ class TextScreen
+ def self.draw (&block)
+ result = Label.new
+ result.instance_eval(&block)
+ result.array.join("") + "\n"
+ end
+ end
+
+ class Label
+
+ def initialize
+ @array = []
+ end
+
+ attr_accessor :array
+
+ def label(text:, border: nil, style: nil)
+ stylish = lambda {return "#{border}#{text.send(style)}#{border}"}
+ not_stylish = lambda {return "#{border}#{text}#{border}"}
+ self.array << not_stylish.call if style == nil
+ self.array << stylish.call if style != nil
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ helper = Label.new
+ helper.instance_eval(&block)
+ if border != nil then helper.array.bordered(border) end
+ if style != nil then helper.array.stylize(style) end
+ self.array << helper.array.map {|element| element + "\n"}
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ helper = Labels.new
+ helper.instance_eval(&block)
+ if border != nil then helper.array.bordered(border) end
+ if style != nil then helper.array.stylize(style) end
+ self.array << helper.array
+ end
+
+ end
+end
+
+class Array
+ def longest_word
+ group_by(&:size).max.last
+ end
+
+ def bordered(sign)
+ max = self.longest_word
+ self.map {|w| w = "#{sign}#{w.append(max.size - w.size)}#{sign}"}
+ end
+
+ def stylize(style)
+ self.map {|word| word.send(style)}
+ end
+end
+
+class String
+ def append(number)
+ self + " " * number
+ end
+end