Решение на Четвърта задача от Александър Пирнарев

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

Към профила на Александър Пирнарев

Резултати

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

Код

module UI
class TextScreen
attr_accessor :temp
def self.draw (&block)
@temp = []
self.instance_eval(&block)
@temp.join('') if @temp.class != String
end
def self.label(text:, style: nil, border: nil)
case style
when :upcase then text = text.upcase
when :downcase then text = text.downcase
end
text = "#{border}#{text}#{border}" if border != nil
@temp << text
end
def self.horizontal(border: nil, style: nil, &block)
self.instance_eval(&block)
horizontal_text = ""
@temp.each do |text|
horizontal_text << text
end
horizontal_text = "#{border}#{horizontaltext}#{border}" if border != nil
@temp = horizontal_text
end
end
end

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

.FFFFF.F.F.

Failures:

  1) Command Line Toolkit adding horizontal group does not change the behavior
     Failure/Error: expect do
       expected #<Proc:0xb90e74dc@/tmp/d20141126-26053-h3nssl/spec.rb:30> to render as "      123\n"
     # /tmp/d20141126-26053-h3nssl/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: vertical do
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:43:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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: vertical do
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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: vertical border: '|' do
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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: vertical style: :upcase do
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:142:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:20:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:20:in `horizontal'
     # /tmp/d20141126-26053-h3nssl/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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: vertical style: :downcase do
     NoMethodError:
       undefined method `vertical' for UI::TextScreen:Class
     # /tmp/d20141126-26053-h3nssl/spec.rb:177:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:20:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:20:in `horizontal'
     # /tmp/d20141126-26053-h3nssl/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-h3nssl/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-h3nssl/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-h3nssl/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.01889 seconds
11 examples, 7 failures

Failed examples:

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

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

Александър обнови решението на 26.11.2014 16:59 (преди почти 10 години)

+module UI
+ class TextScreen
+ attr_accessor :temp
+ def self.draw (&block)
+ @temp = []
+ self.instance_eval(&block)
+ @temp.join('') if @temp.class != String
+ end
+
+ def self.label(text:, style: nil, border: nil)
+ case style
+ when :upcase then text = text.upcase
+ when :downcase then text = text.downcase
+ end
+ text = "#{border}#{text}#{border}" if border != nil
+ @temp << text
+ end
+
+ def self.horizontal(border: nil, style: nil, &block)
+ self.instance_eval(&block)
+ horizontal_text = ""
+ @temp.each do |text|
+ horizontal_text << text
+ end
+ horizontal_text = "#{border}#{horizontaltext}#{border}" if border != nil
+ @temp = horizontal_text
+ end
+ end
+end

def max_length(strings)

        max = strings.last
        strings.each do | element |
            max = element if max.length < element.length
        end
        max.length
    end

    def self.vertical(border: nil, &block)
        self.instance_eval(&block)
        @temp.map! { | text | text + "\n" }
        if border != nil
            border_vertical(@temp, max_length(@temp), border)
        end
    end

    def border_vertical(strings, max_length, border)
        strings.each do | element |
         (max_length - element.length).times { element << " " } 
        end
        strings.each do | element |
         "#{border}#{element}#{border}" 
        end
    end