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

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

Към профила на Любомир Иванов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 10 неуспешни тест(а)

Код

module UI
class TextScreen
def self.label(text: '', border: nil, style: nil)
text = border + text + border if border
if style
@words_with_style << @counter_of_words
text = text.send style
end
Utility.final_operations(self, text)
end
attr_accessor :counter_of_words, :words_with_style, :text ,:group, :results
def self.has_border(border)
max_len, temp = @text.longest_word.join.length, []
@text.each do |n|
n = Utility.multiple((max_len - n.length), n)
n = border + n + border
temp << n
end
@text = temp
end
def self.has_style(style)
temp = []
@text.each_with_index do |n, i|
temp = Utility.is_with_style(@words_with_style, [temp, n, style])
end
@text = temp
end
def self.vertical(border: nil, style: nil, &block)
instance_eval(&block)
Utility.vertical_styling(self, border, style)
end
def self.horizontal(border: nil, style: nil, &block)
instance_eval(&block)
Utility.horizontal_styling(self, border, style)
end
def self.draw(&block)
@counter_of_words = 0
@words_with_style = []
@text = []
@group = []
@results = []
instance_eval(&block)
end
end
class Utility
def self.is_with_style(words_with_style, array )
array[0] << array[1].send(array[2]) if words_with_style.include? array[1]
array[0] << array[1] unless words_with_style.include? array[1]
array[0]
end
def self.final_operations(obj, text)
obj.counter_of_words += 1
obj.text << text
obj.text.join
end
def self.vertical_styling(obj, border, style)
TextScreen.has_border(border) if border
TextScreen.has_style(style) if style
obj.group << obj.text.join("\n") unless obj.text.empty?
obj.text, obj.counter_of_words = [], 0
obj.group.join("\n")
end
def self.horizontal_styling(obj, border, style)
TextScreen.has_style(style) if style
obj.group << obj.text.join unless obj.text.empty?
obj.text, obj.counter_of_words = [], 0
obj.group.join
end
def self.multiple(count, n)
count.times { n += " "}
n
end
end
end
class Array
def longest_word
group_by(&:size).max.last
end
end

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

FFFFFFFFFF.

Failures:

  1) Command Line Toolkit arranges components horizontally by default
     Failure/Error: label text: '1'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:21:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:20: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 adding horizontal group does not change the behavior
     Failure/Error: label text: '1'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:32:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `horizontal'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:31:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  3) Command Line Toolkit verical group orders elements vertically
     Failure/Error: label text: '1'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:44:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `vertical'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:43:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  4) Command Line Toolkit handles complex group nestings
     Failure/Error: label text: '1'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:59:in `block (5 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `horizontal'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:58:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `vertical'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  5) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: label text: 'something'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:87:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `vertical'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  6) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: label text: '1', border: '|'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:102:in `block (5 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `horizontal'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:101:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:33:in `vertical'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  7) Command Line Toolkit applies upcase to simple components
     Failure/Error: label text: 'some', style: :upcase
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:129:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:128: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)>'

  8) Command Line Toolkit propagates upcase to child components
     Failure/Error: label text: 'some'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:139:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `horizontal'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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)>'

  9) Command Line Toolkit applies downcase to simple components
     Failure/Error: label text: 'SOME'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:164:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:163: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)>'

  10) Command Line Toolkit propagates downcase to child components
     Failure/Error: label text: 'SOME'
     NoMethodError:
       undefined method `counter_of_words' for UI::TextScreen:Class
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:61:in `final_operations'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:9:in `label'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:174:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:38:in `horizontal'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `instance_eval'
     # /tmp/d20141126-26053-1nj3c7z/solution.rb:48:in `draw'
     # /tmp/d20141126-26053-1nj3c7z/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1nj3c7z/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.01683 seconds
11 examples, 10 failures

Failed examples:

rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:19 # Command Line Toolkit arranges components horizontally by default
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:41 # Command Line Toolkit verical group orders elements vertically
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:84 # Command Line Toolkit wraps vertically-aligned components correctly in border
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:127 # Command Line Toolkit applies upcase to simple components
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:162 # Command Line Toolkit applies downcase to simple components
rspec /tmp/d20141126-26053-1nj3c7z/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

Любомир обнови решението на 26.11.2014 16:37 (преди почти 10 години)

+module UI
+ class TextScreen
+ def self.label(text: '', border: nil, style: nil)
+ text = border + text + border if border
+ if style
+ @words_with_style << @counter_of_words
+ text = text.send style
+ end
+ Utility.final_operations(self, text)
+ end
+
+ attr_accessor :counter_of_words, :words_with_style, :text ,:group, :results
+
+ def self.has_border(border)
+ max_len, temp = @text.longest_word.join.length, []
+ @text.each do |n|
+ n = Utility.multiple((max_len - n.length), n)
+ n = border + n + border
+ temp << n
+ end
+ @text = temp
+ end
+
+ def self.has_style(style)
+ temp = []
+ @text.each_with_index do |n, i|
+ temp = Utility.is_with_style(@words_with_style, [temp, n, style])
+ end
+ @text = temp
+ end
+
+ def self.vertical(border: nil, style: nil, &block)
+ instance_eval(&block)
+ Utility.vertical_styling(self, border, style)
+ end
+
+ def self.horizontal(border: nil, style: nil, &block)
+ instance_eval(&block)
+ Utility.horizontal_styling(self, border, style)
+ end
+
+ def self.draw(&block)
+ @counter_of_words = 0
+ @words_with_style = []
+ @text = []
+ @group = []
+ @results = []
+ instance_eval(&block)
+ end
+
+ end
+
+ class Utility
+ def self.is_with_style(words_with_style, array )
+ array[0] << array[1].send(array[2]) if words_with_style.include? array[1]
+ array[0] << array[1] unless words_with_style.include? array[1]
+ array[0]
+ end
+
+ def self.final_operations(obj, text)
+ obj.counter_of_words += 1
+ obj.text << text
+ obj.text.join
+ end
+
+ def self.vertical_styling(obj, border, style)
+ has_border(border) if border
+ has_style(style) if style
+ obj.group << obj.text.join("\n") unless obj.text.empty?
+ obj.text, obj.counter_of_words = [], 0
+ obj.group.join("\n")
+ end
+
+ def self.horizontal_styling(obj, border, style)
+ has_style(style) if style
+ obj.group << obj.text.join unless obj.text.empty?
+ obj.text, obj.counter_of_words = [], 0
+ obj.group.join
+ end
+
+ def self.multiple(count, n)
+ count.times { n += " "}
+ n
+ end
+ end
+end
+
+class Array
+ def longest_word
+ group_by(&:size).max.last
+ end
+end

Любомир обнови решението на 26.11.2014 16:55 (преди почти 10 години)

module UI
class TextScreen
def self.label(text: '', border: nil, style: nil)
text = border + text + border if border
if style
@words_with_style << @counter_of_words
text = text.send style
end
Utility.final_operations(self, text)
end
attr_accessor :counter_of_words, :words_with_style, :text ,:group, :results
def self.has_border(border)
max_len, temp = @text.longest_word.join.length, []
@text.each do |n|
n = Utility.multiple((max_len - n.length), n)
n = border + n + border
temp << n
end
@text = temp
end
def self.has_style(style)
temp = []
@text.each_with_index do |n, i|
temp = Utility.is_with_style(@words_with_style, [temp, n, style])
end
@text = temp
end
def self.vertical(border: nil, style: nil, &block)
instance_eval(&block)
Utility.vertical_styling(self, border, style)
end
def self.horizontal(border: nil, style: nil, &block)
instance_eval(&block)
Utility.horizontal_styling(self, border, style)
end
def self.draw(&block)
@counter_of_words = 0
@words_with_style = []
@text = []
@group = []
@results = []
instance_eval(&block)
end
end
class Utility
def self.is_with_style(words_with_style, array )
array[0] << array[1].send(array[2]) if words_with_style.include? array[1]
array[0] << array[1] unless words_with_style.include? array[1]
array[0]
end
def self.final_operations(obj, text)
obj.counter_of_words += 1
obj.text << text
obj.text.join
end
def self.vertical_styling(obj, border, style)
- has_border(border) if border
- has_style(style) if style
+ TextScreen.has_border(border) if border
+ TextScreen.has_style(style) if style
obj.group << obj.text.join("\n") unless obj.text.empty?
obj.text, obj.counter_of_words = [], 0
obj.group.join("\n")
end
def self.horizontal_styling(obj, border, style)
- has_style(style) if style
+ TextScreen.has_style(style) if style
obj.group << obj.text.join unless obj.text.empty?
obj.text, obj.counter_of_words = [], 0
obj.group.join
end
def self.multiple(count, n)
count.times { n += " "}
n
end
end
end
class Array
def longest_word
group_by(&:size).max.last
end
end