Решение на Четвърта задача от Яни Малцев

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

Към профила на Яни Малцев

Резултати

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

Код

module UI
module TextScreen
def self.draw(style: nil, border: "", &block)
proxy = Proxy.new(style: style)
proxy.instance_eval(&block)
result = border + proxy.result + border
end
end
class Proxy
attr_accessor :result
def initialize (style:)
@result = ""
@style = style
end
def horizontal(style: nil, border: "", &block)
style = @style if style == nil
helper = Horizontal.new(style: style)
helper.instance_eval(&block)
@result = @result + border + helper.result + border
end
def vertical(style: nil, border: "", &block)
style = @style if style == nil
helper = Vertical.new(style: style, border: border)
helper.instance_eval(&block)
@result = @result + helper.concatenate
end
def label(text: "", style: nil, border: "")
style = @style if style == nil
text = text.downcase if style == :downcase
text = text.upcase if style == :upcase
@result = @result + border + text + border
end
end
class Horizontal < Proxy
end
class Vertical < Proxy
attr_accessor :parts
def initialize (style:, border:)
super(style: style)
@parts = []
@border = border
end
def horizontal(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def vertical(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def label(text: "", style: nil, border: "")
@result = ""
@parts << super(text: text, style: style, border: border)
end
def concatenate
maximum = @parts.collect { |x| x = x.length }.max
result = @parts.collect{ |x|
x = @border + x.ljust(maximum) + @border + "\n"
}.join
end
end
end

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

...F.F.F.F.

Failures:

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

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

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

Failed examples:

rspec /tmp/d20141126-26053-1qyxxsf/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1qyxxsf/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1qyxxsf/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1qyxxsf/spec.rb:171 # Command Line Toolkit propagates downcase to child components

История (5 версии и 3 коментара)

Яни обнови решението на 23.11.2014 17:19 (преди около 10 години)

+module UI
+ module TextScreen
+ def self.draw(style: nil, border: "", &block)
+ proxy = Proxy.new(style: style)
+ proxy.instance_eval(&block)
+ string = border + proxy.string + border
+ end
+ end
+
+ class Proxy
+ attr_accessor :result
+
+ def initialize (style:)
+ @result = ""
+ @style = style
+ end
+
+ def horizontal(style: nil, border: "", &block)
+ style = @style if style == nil
+ helper = Horizontal.new(style: style)
+ helper.instance_eval(&block)
+ @result = @result + border + helper.result + border
+ end
+
+ def vertical(style: nil, border: "", &block)
+ style = @style if style == nil
+ helper = Vertical.new(style: style, border: border)
+ helper.instance_eval(&block)
+ @result = @result + helper.concatenate
+ end
+
+ def label(text: "", style: nil, border: "")
+ style = @style if style == nil
+ text = text.downcase if style == :downcase
+ text = text.upcase if style == :upcase
+ @result = @result + border + text + border
+ end
+ end
+
+ class Horizontal < Proxy
+
+ end
+
+ class Vertical < Proxy
+ attr_accessor :parts
+
+ def initialize (style:, border:)
+ super(style: style)
+ @parts = []
+ @border = border
+ end
+
+ def horizontal(style: nil, border: "", &block)
+ @result = ""
+ @parts << super(style: style, border: border, &block)
+ end
+
+ def vertical(style: nil, border: "", &block)
+ @result = ""
+ @parts << super(style: style, border: border, &block)
+ end
+
+ def label(text: "", style: nil, border: "")
+ @result = ""
+ @parts << super(text: text, style: style, border: border)
+ end
+
+ def concatenate
+ maximum = @parts.collect { |x| x = x.length }.max
+ result = @parts.collect{ |x|
+ x = @border + x.ljust(maximum) + @border + "\n"
+ }.join
+ end
+ end
+end

Яни обнови решението на 23.11.2014 17:23 (преди около 10 години)

module UI
module TextScreen
def self.draw(style: nil, border: "", &block)
proxy = Proxy.new(style: style)
proxy.instance_eval(&block)
- string = border + proxy.string + border
+ string = border + proxy.string + border
end
end
class Proxy
attr_accessor :result
def initialize (style:)
- @result = ""
- @style = style
- end
+ @result = ""
+ @style = style
+ end
def horizontal(style: nil, border: "", &block)
- style = @style if style == nil
+ style = @style if style == nil
helper = Horizontal.new(style: style)
- helper.instance_eval(&block)
+ helper.instance_eval(&block)
@result = @result + border + helper.result + border
end
def vertical(style: nil, border: "", &block)
- style = @style if style == nil
+ style = @style if style == nil
helper = Vertical.new(style: style, border: border)
- helper.instance_eval(&block)
- @result = @result + helper.concatenate
+ helper.instance_eval(&block)
+ @result = @result + helper.concatenate
end
- def label(text: "", style: nil, border: "")
- style = @style if style == nil
+ def label(text: "", style: nil, border: "")
+ style = @style if style == nil
text = text.downcase if style == :downcase
- text = text.upcase if style == :upcase
- @result = @result + border + text + border
+ text = text.upcase if style == :upcase
+ @result = @result + border + text + border
end
end
class Horizontal < Proxy
end
class Vertical < Proxy
- attr_accessor :parts
+ attr_accessor :parts
def initialize (style:, border:)
super(style: style)
- @parts = []
- @border = border
+ @parts = []
+ @border = border
end
def horizontal(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def vertical(style: nil, border: "", &block)
- @result = ""
+ @result = ""
@parts << super(style: style, border: border, &block)
end
def label(text: "", style: nil, border: "")
- @result = ""
+ @result = ""
@parts << super(text: text, style: style, border: border)
end
def concatenate
maximum = @parts.collect { |x| x = x.length }.max
result = @parts.collect{ |x|
x = @border + x.ljust(maximum) + @border + "\n"
- }.join
+ }.join
end
end
end

Яни обнови решението на 23.11.2014 17:25 (преди около 10 години)

module UI
module TextScreen
def self.draw(style: nil, border: "", &block)
proxy = Proxy.new(style: style)
proxy.instance_eval(&block)
string = border + proxy.string + border
end
end
class Proxy
attr_accessor :result
def initialize (style:)
@result = ""
@style = style
end
def horizontal(style: nil, border: "", &block)
style = @style if style == nil
helper = Horizontal.new(style: style)
helper.instance_eval(&block)
@result = @result + border + helper.result + border
end
- def vertical(style: nil, border: "", &block)
+ def vertical(style: nil, border: "", &block)
style = @style if style == nil
helper = Vertical.new(style: style, border: border)
helper.instance_eval(&block)
@result = @result + helper.concatenate
end
def label(text: "", style: nil, border: "")
style = @style if style == nil
text = text.downcase if style == :downcase
text = text.upcase if style == :upcase
@result = @result + border + text + border
end
end
class Horizontal < Proxy
end
class Vertical < Proxy
attr_accessor :parts
def initialize (style:, border:)
super(style: style)
@parts = []
@border = border
end
def horizontal(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def vertical(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def label(text: "", style: nil, border: "")
@result = ""
@parts << super(text: text, style: style, border: border)
end
- def concatenate
- maximum = @parts.collect { |x| x = x.length }.max
- result = @parts.collect{ |x|
- x = @border + x.ljust(maximum) + @border + "\n"
+ def concatenate
+ maximum = @parts.collect { |x| x = x.length }.max
+ result = @parts.collect{ |x|
+ x = @border + x.ljust(maximum) + @border + "\n"
}.join
end
end
end

Яни обнови решението на 23.11.2014 17:25 (преди около 10 години)

module UI
module TextScreen
def self.draw(style: nil, border: "", &block)
proxy = Proxy.new(style: style)
proxy.instance_eval(&block)
string = border + proxy.string + border
end
end
class Proxy
attr_accessor :result
def initialize (style:)
@result = ""
@style = style
end
def horizontal(style: nil, border: "", &block)
style = @style if style == nil
helper = Horizontal.new(style: style)
helper.instance_eval(&block)
@result = @result + border + helper.result + border
end
def vertical(style: nil, border: "", &block)
style = @style if style == nil
helper = Vertical.new(style: style, border: border)
helper.instance_eval(&block)
@result = @result + helper.concatenate
end
def label(text: "", style: nil, border: "")
style = @style if style == nil
text = text.downcase if style == :downcase
text = text.upcase if style == :upcase
@result = @result + border + text + border
end
end
class Horizontal < Proxy
end
class Vertical < Proxy
attr_accessor :parts
def initialize (style:, border:)
super(style: style)
@parts = []
@border = border
end
def horizontal(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def vertical(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def label(text: "", style: nil, border: "")
@result = ""
@parts << super(text: text, style: style, border: border)
end
def concatenate
maximum = @parts.collect { |x| x = x.length }.max
result = @parts.collect{ |x|
x = @border + x.ljust(maximum) + @border + "\n"
}.join
- end
+ end
end
end

Яни обнови решението на 23.11.2014 17:41 (преди около 10 години)

module UI
module TextScreen
def self.draw(style: nil, border: "", &block)
proxy = Proxy.new(style: style)
proxy.instance_eval(&block)
- string = border + proxy.string + border
+ result = border + proxy.result + border
end
end
class Proxy
attr_accessor :result
def initialize (style:)
@result = ""
@style = style
end
def horizontal(style: nil, border: "", &block)
style = @style if style == nil
helper = Horizontal.new(style: style)
helper.instance_eval(&block)
@result = @result + border + helper.result + border
end
def vertical(style: nil, border: "", &block)
style = @style if style == nil
helper = Vertical.new(style: style, border: border)
helper.instance_eval(&block)
@result = @result + helper.concatenate
end
def label(text: "", style: nil, border: "")
style = @style if style == nil
text = text.downcase if style == :downcase
text = text.upcase if style == :upcase
@result = @result + border + text + border
end
end
class Horizontal < Proxy
end
class Vertical < Proxy
attr_accessor :parts
def initialize (style:, border:)
super(style: style)
@parts = []
@border = border
end
def horizontal(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def vertical(style: nil, border: "", &block)
@result = ""
@parts << super(style: style, border: border, &block)
end
def label(text: "", style: nil, border: "")
@result = ""
@parts << super(text: text, style: style, border: border)
end
def concatenate
maximum = @parts.collect { |x| x = x.length }.max
result = @parts.collect{ |x|
x = @border + x.ljust(maximum) + @border + "\n"
}.join
end
end
end

Не си поправяй стила в source control-а (тук), а в редактора ти. :)

Прегледай темата във форума и примерите, които обсъждаме там.

Не ми харесва как си форматирал извикването на collect. Ползваме do/end за многоредови болове. Даването на стойност на x не е нужно. Предпочитаме синонима map тъй като е по-често ползван.

Знанието за създаване на елементи и слагане на рамка го има на две места. Може ли да се пакетира на едно място? Може би Horizontal и Vertical не са толкова различни. На едното място пазиш елементите в String, а на другото в Array. Можеш ли да ги направиш по-консистентни? Така може би ще можеш да извлечеш общата им логика на едно място.

За жалост поправките в стила не ги забелязвам докато не пусна решението тук, защото на моя редактор табулациите(които се слагат автоматично при нов ред) са 4 интервала, а тук е 8.

Отколкото до забележките по кода, разбирам как може да се направи, но просто не разполагам с времето да ги поправя до крайния срок. Много жалко.