- Коректно
- 4 успешни тест(а)
- 0 неуспешни тест(а)
.... Finished in 0.00834 seconds 4 examples, 0 failures
Срокът за предаване на решения е отминал
Често използван идиом в Ruby са така наречените клас макроси. Това предизвикателство ще ви даде възможност да имплементирате свое такова.
Най-известният пример за използване на клас макроси са attr_accessor
, attr_reader
, attr_writer
, пример.
Задачата ви е да разширите Class
с макро attr_initializer
.
То трябва да приема произволен брой символи като аргументи и да се използва в класове, в които с аргументите на initialize
се инициализират атрибути със същите имена.
Пример:
class Point
attr_initializer :x, :y
attr_reader :x, :y
end
Горната дефиниция трябва да е еквивалентна на
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
end
Нека при подаване на грешен брой аргументи при инициализиране на обект от клас, използващ това макро, да се вдига ArgumentError
. Очакваме съобщението на грешката да е стандартното такова за подаване на грешен брой аргументи: "wrong number of arguments (number of passed arguments here
for number of expected arguments here
)".
За информация как да изпълните примерния тест, погледнете GitHub хранилището с домашните.
.... Finished in 0.00834 seconds 4 examples, 0 failures
.... Finished in 0.0068 seconds 4 examples, 0 failures
.... Finished in 0.00673 seconds 4 examples, 0 failures
.... Finished in 0.00682 seconds 4 examples, 0 failures
.... Finished in 0.00803 seconds 4 examples, 0 failures
.... Finished in 0.00683 seconds 4 examples, 0 failures
.... Finished in 0.00679 seconds 4 examples, 0 failures
.... Finished in 0.00681 seconds 4 examples, 0 failures
.... Finished in 0.00674 seconds 4 examples, 0 failures
.... Finished in 0.00683 seconds 4 examples, 0 failures
.... Finished in 0.00678 seconds 4 examples, 0 failures
.... Finished in 0.00774 seconds 4 examples, 0 failures
.... Finished in 0.00685 seconds 4 examples, 0 failures
.... Finished in 0.0075 seconds 4 examples, 0 failures
/tmp/d20141122-26053-srql2u/solution.rb:2:in `attr_initializer': wrong number of arguments (3 for 2) (ArgumentError) from /tmp/d20141122-26053-srql2u/spec.rb:13:in `<class:Person>' from /tmp/d20141122-26053-srql2u/spec.rb:12:in `block in <top (required)>' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/dsl.rb:18:in `describe' from /tmp/d20141122-26053-srql2u/spec.rb:1:in `<top (required)>' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
.... Finished in 0.0067 seconds 4 examples, 0 failures
.... Finished in 0.00681 seconds 4 examples, 0 failures
.... Finished in 0.00962 seconds 4 examples, 0 failures
.... Finished in 0.00681 seconds 4 examples, 0 failures
/tmp/d20141122-26053-cjz3p8/solution.rb:2:in `attr_initializer': wrong number of arguments (3 for 2) (ArgumentError) from /tmp/d20141122-26053-cjz3p8/spec.rb:13:in `<class:Person>' from /tmp/d20141122-26053-cjz3p8/spec.rb:12:in `block in <top (required)>' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/dsl.rb:18:in `describe' from /tmp/d20141122-26053-cjz3p8/spec.rb:1:in `<top (required)>' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run' from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
.... Finished in 0.00681 seconds 4 examples, 0 failures
.... Finished in 0.00686 seconds 4 examples, 0 failures
.... Finished in 0.008 seconds 4 examples, 0 failures
.... Finished in 0.00689 seconds 4 examples, 0 failures
.... Finished in 0.00666 seconds 4 examples, 0 failures
.... Finished in 0.00661 seconds 4 examples, 0 failures
.... Finished in 0.00744 seconds 4 examples, 0 failures
.... Finished in 0.00812 seconds 4 examples, 0 failures
#<FunctionCall:0xb96ae0a0>F.FF Failures: 1) Class.attr_initializer can be used for initialization of attributes Failure/Error: expect(FunctionCall.new(:display, [2]).function).to eq(:display) expected: :display got: nil (compared using ==) # /tmp/d20141122-26053-1tqpoxm/spec.rb:18: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) Class.attr_initializer assigns correct values to fields Failure/Error: person = Person.new(22, :male, "Sofia") NameError: undefined local variable or method `male' for #<Person:0xb96a2a70 @age=22> # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `instance_eval' # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `instance_eval' # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `block (2 levels) in attr_initializer' # /tmp/d20141122-26053-1tqpoxm/solution.rb:7:in `each' # /tmp/d20141122-26053-1tqpoxm/solution.rb:7:in `block in attr_initializer' # /tmp/d20141122-26053-1tqpoxm/spec.rb:28:in `new' # /tmp/d20141122-26053-1tqpoxm/spec.rb:28: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) Class.attr_initializer doesn't expose attributes with reader/writer macros Failure/Error: expect { Assignment.new(:x, 4).target }.to raise_error(NoMethodError) expected NoMethodError, got #<NameError: undefined local variable or method `x' for #<Assignment:0xb96a14cc>> with backtrace: # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `instance_eval' # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `instance_eval' # /tmp/d20141122-26053-1tqpoxm/solution.rb:8:in `block (2 levels) in attr_initializer' # /tmp/d20141122-26053-1tqpoxm/solution.rb:7:in `each' # /tmp/d20141122-26053-1tqpoxm/solution.rb:7:in `block in attr_initializer' # /tmp/d20141122-26053-1tqpoxm/spec.rb:36:in `new' # /tmp/d20141122-26053-1tqpoxm/spec.rb:36:in `block (3 levels) in <top (required)>' # /tmp/d20141122-26053-1tqpoxm/spec.rb:36: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)>' # /tmp/d20141122-26053-1tqpoxm/spec.rb:36: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.00849 seconds 4 examples, 3 failures Failed examples: rspec /tmp/d20141122-26053-1tqpoxm/spec.rb:17 # Class.attr_initializer can be used for initialization of attributes rspec /tmp/d20141122-26053-1tqpoxm/spec.rb:27 # Class.attr_initializer assigns correct values to fields rspec /tmp/d20141122-26053-1tqpoxm/spec.rb:35 # Class.attr_initializer doesn't expose attributes with reader/writer macros
.... Finished in 0.00673 seconds 4 examples, 0 failures
.... Finished in 0.00668 seconds 4 examples, 0 failures
.... Finished in 0.0068 seconds 4 examples, 0 failures
.F.F Failures: 1) Class.attr_initializer can be used to detect wrong number of arguments in initialization Failure/Error: expect { FunctionCall.new(:fail) }.to raise_error(ArgumentError, "wrong number of arguments (1 for 2)") expected ArgumentError with "wrong number of arguments (1 for 2)", got #<ArgumentError: wrong number of arguments(1 for 2)> with backtrace: # (eval):2:in `initialize' # /tmp/d20141122-26053-17vkykb/spec.rb:23:in `new' # /tmp/d20141122-26053-17vkykb/spec.rb:23:in `block (3 levels) in <top (required)>' # /tmp/d20141122-26053-17vkykb/spec.rb:23: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)>' # /tmp/d20141122-26053-17vkykb/spec.rb:23: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) Class.attr_initializer doesn't expose attributes with reader/writer macros Failure/Error: expect { Assignment.new(:x, 4).target }.to raise_error(NoMethodError) expected NoMethodError but nothing was raised # /tmp/d20141122-26053-17vkykb/spec.rb:36: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.00767 seconds 4 examples, 2 failures Failed examples: rspec /tmp/d20141122-26053-17vkykb/spec.rb:22 # Class.attr_initializer can be used to detect wrong number of arguments in initialization rspec /tmp/d20141122-26053-17vkykb/spec.rb:35 # Class.attr_initializer doesn't expose attributes with reader/writer macros