Решение на Четвърта задача от Стилиян Стоянов

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

Към профила на Стилиян Стоянов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 6 успешни тест(а)
  • 5 неуспешни тест(а)

Код

module UI
def vertical(border: nil, style: nil, &block)
vertical_instance = Vertical.new(border, style)
@string = @string + vertical_instance.instance_eval(&block)
end
def horizontal(border: nil, style: nil, &block)
horizontal_instance = Horizontal.new(border, style)
@string = @string + horizontal_instance.instance_eval(&block)
@string = add_border(@string, border)
end
def apply_style(text_string, style)
if style == :upcase
return text_string.upcase
elsif style == :downcase
return text_string.downcase
else text_string
end
end
def add_border(text_string, border)
text_string = border.to_s + text_string + border.to_s
end
class TextScreen
extend UI
def self.draw(&block)
@string = ""
self.class_eval(&block)
end
def self.label(text:, border: nil, style: nil)
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text
end
end
class Vertical
include UI
attr_accessor :border, :style, :longest_text
def initialize(border, style)
@border = border
@style = style
@string = ""
@longest_text = 0
end
def label(text:, border: self.border, style: self.style)
if (text.size > @longest_text)
@longest_text = text.size
end
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text + "\n"
end
def horizontal(border: nil, style: nil, &block)
horizon = UI::Horizontal.new(border, style)
if @string == ""
then @string = horizon.instance_eval(&block)
else
@string = @string + "\n" + horizon.instance_eval(&block)
end
end
end
class Horizontal
include UI
attr_accessor :border, :style
def initialize(border, style)
@border = border
@style = style
@string = ""
end
def horizontal(border: nil, style: nil, &block)
horizontal_instance = Horizontal.new(border, style)
@string = @string + horizontal_instance.instance_eval(&block)
end
def label(text:, border: self.border, style: self.style)
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text
end
end
end

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

...FFF.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9e5270c@/tmp/d20141126-26053-arruu4/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-arruu4/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)>'

  2) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: expect do
       expected #<Proc:0xb9e4b2e0@/tmp/d20141126-26053-arruu4/spec.rb:85> to render as "      |something|\n      |some     |\n      |soommee  |\n"
     # /tmp/d20141126-26053-arruu4/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)>'

  3) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9e35468@/tmp/d20141126-26053-arruu4/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-arruu4/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)>'

  4) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9e220ac@/tmp/d20141126-26053-arruu4/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-arruu4/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)>'

  5) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9e10ba4@/tmp/d20141126-26053-arruu4/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-arruu4/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.02404 seconds
11 examples, 5 failures

Failed examples:

rspec /tmp/d20141126-26053-arruu4/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-arruu4/spec.rb:84 # Command Line Toolkit wraps vertically-aligned components correctly in border
rspec /tmp/d20141126-26053-arruu4/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-arruu4/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-arruu4/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

Стилиян обнови решението на 26.11.2014 16:18 (преди почти 10 години)

+module UI
+
+ def vertical(border: nil, style: nil, &block)
+ vertical_instance = Vertical.new(border, style)
+ @string = @string + vertical_instance.instance_eval(&block)
+
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ horizontal_instance = Horizontal.new(border, style)
+ @string = @string + horizontal_instance.instance_eval(&block)
+ @string = add_border(@string, border)
+ end
+
+ def apply_style(text_string, style)
+ if style == :upcase
+ return text_string.upcase
+ elsif style == :downcase
+ return text_string.downcase
+ else text_string
+ end
+ end
+
+ def add_border(text_string, border)
+ text_string = border.to_s + text_string + border.to_s
+ end
+
+ class TextScreen
+ extend UI
+
+ def self.draw(&block)
+ @string = ""
+ self.class_eval(&block)
+ end
+
+ def self.label(text:, border: nil, style: nil)
+ text = apply_style(text, style)
+ text = add_border(text, border)
+ @string = @string + text
+ end
+ end
+
+ class Vertical
+ include UI
+
+ attr_accessor :border, :style
+ def initialize(border, style)
+ @border = border
+ @style = style
+ @string = ""
+ @longest_text = 0
+ end
+ def label(text:, border: self.border, style: self.style)
+ if (text.size > @longest_text)
+ @longest_text = text.size
+ p @longest_text
+ end
+ text = apply_style(text, style)
+ text = add_border(text, border)
+ @string = @string + text + "\n"
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ horizon = UI::Horizontal.new(border, style)
+ if @string == ""
+ then @string = horizon.instance_eval(&block)
+ else
+ @string = @string + "\n" + hor.instance_eval(&block)
+ end
+ end
+ end
+
+ class Horizontal
+ include UI
+ attr_accessor :border, :style
+ def initialize(border, style)
+ @border = border
+ @style = style
+ @string = ""
+ end
+
+ def label(text:, border: self.border, style: self.style)
+ text = apply_style(text, style)
+ text = add_border(text, border)
+ @string = @string + text
+ end
+ end
+end

Стилиян обнови решението на 26.11.2014 16:54 (преди почти 10 години)

module UI
def vertical(border: nil, style: nil, &block)
vertical_instance = Vertical.new(border, style)
@string = @string + vertical_instance.instance_eval(&block)
-
end
def horizontal(border: nil, style: nil, &block)
horizontal_instance = Horizontal.new(border, style)
@string = @string + horizontal_instance.instance_eval(&block)
@string = add_border(@string, border)
end
def apply_style(text_string, style)
if style == :upcase
return text_string.upcase
elsif style == :downcase
return text_string.downcase
else text_string
end
end
def add_border(text_string, border)
- text_string = border.to_s + text_string + border.to_s
+ text_string = border.to_s + text_string + border.to_s
end
-
class TextScreen
- extend UI
+ extend UI
- def self.draw(&block)
- @string = ""
- self.class_eval(&block)
- end
+ def self.draw(&block)
+ @string = ""
+ self.class_eval(&block)
+ end
def self.label(text:, border: nil, style: nil)
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text
end
end
class Vertical
- include UI
-
- attr_accessor :border, :style
+ include UI
+ attr_accessor :border, :style, :longest_text
def initialize(border, style)
- @border = border
- @style = style
- @string = ""
- @longest_text = 0
+ @border = border
+ @style = style
+ @string = ""
+ @longest_text = 0
end
def label(text:, border: self.border, style: self.style)
if (text.size > @longest_text)
- @longest_text = text.size
- p @longest_text
+ @longest_text = text.size
end
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text + "\n"
- end
+ end
def horizontal(border: nil, style: nil, &block)
horizon = UI::Horizontal.new(border, style)
if @string == ""
then @string = horizon.instance_eval(&block)
else
- @string = @string + "\n" + hor.instance_eval(&block)
+ @string = @string + "\n" + horizon.instance_eval(&block)
end
end
end
class Horizontal
- include UI
- attr_accessor :border, :style
- def initialize(border, style)
- @border = border
- @style = style
- @string = ""
- end
+ include UI
+ attr_accessor :border, :style
+ def initialize(border, style)
+ @border = border
+ @style = style
+ @string = ""
+ end
- def label(text:, border: self.border, style: self.style)
+ def horizontal(border: nil, style: nil, &block)
+ horizontal_instance = Horizontal.new(border, style)
+ @string = @string + horizontal_instance.instance_eval(&block)
+ end
+
+ def label(text:, border: self.border, style: self.style)
text = apply_style(text, style)
text = add_border(text, border)
@string = @string + text
- end
+ end
end
end