Мемоизация

Краен срок
17.11.2013 12:00

Срокът за предаване на решения е отминал

Мемоизация

Създайте клас Memoizer, чийто конструктор приема като единствен аргумент какъвто и да е обект. Memoizer трябва да работи като прокси, тоест трябва да:

...отговаря на същите методи, като подадената инстанция и когато тези методи бъдат извикани, да връщат същите стойности, каквито биха върнали, ако бяха извикани директно върху инстанцията и тъй нататък.

Memoizer трябва също така да кешира резултатите от извиканите му методи и при повторно извикване на вече извикан метод със същите аргументи, да не се обръща към методите на подадената инстанция, а да връща резултата от кеша си.

Пример:

string = "Remember"
memoizer = Memoizer.new string

memoizer.length # => 8, извиква string.length
memoizer.length # => 8, не вика string.length

Уточнения:

  1. Memoizer не кешира резултати за методи, на които е подаден блок. Вместо това, в такива случаи се държи като обикновено прокси.
  2. Ако Memoizer бъде извикан с несъществуващ в подадената инстанция метод, очаквано следва да се хвърли NoMethodError.
  3. Memoizer ще бъде тестван само с public методи. Какво трябва да прави в други случаи, според вас, може да се обсъди в темата във форума. :-)
  4. Ако примерният тест ви се струва постничък, то бъдете сигурни, че това е нарочно и повече няма да получите. :-)

Решения

Георги Шопов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Шопов
class Memoizer < BasicObject
def initialize(target)
@target = target
@cache = ::Hash.new({})
end
def method_missing(method, *args, &block)
if @target.respond_to?(method)
respond_to_public(method, args, &block)
else
::Kernel.send :raise, ::NoMethodError
end
end
private
def respond_to_public(method, args, &block)
if @cache.include?(method) and @cache[method].include?(args) and
not ::Kernel.block_given?
@cache[method][args]
else
call_return_value = @target.public_send method, *args, &block
unless ::Kernel.block_given?
@cache[method] = { args => call_return_value }
end
call_return_value
end
end
end
......

Finished in 0.01284 seconds
6 examples, 0 failures
Георги Ангелов
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Георги Ангелов
class Memoizer < BasicObject
def initialize(object)
@object = object
@cache = {}
end
def method_missing(name, *arguments, &block)
call_key = [name, arguments]
call_result = @cache[call_key]
if call_result.nil? or ::Kernel.block_given?
call_result = @object.public_send name, *arguments, &block
@cache[call_key] = call_result unless ::Kernel.block_given?
end
call_result
end
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-5zatrf/spec.rb:48: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.0152 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-5zatrf/spec.rb:35 # Memoizer works with nil return values
Георги Кръстев
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Кръстев
class Memoizer < BasicObject
def initialize(target)
@target = target
@cache = ::Hash.new do |cache, message|
cache[message] = @target.public_send(*message)
end
end
private
def method_missing(name, *args, &block)
block ? @target.public_send(name, *args, &block) : @cache[[name, *args]]
end
end
......

Finished in 0.01184 seconds
6 examples, 0 failures
Стефан Василев
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Стефан Василев
class Memoizer < BasicObject
class Error < ::NoMethodError
end
def initialize(target)
@target = target
@calls = {}
end
def method_missing(method, *args, &block)
if @target.respond_to? method
return @target.public_send method, *args, &block unless block == nil
return @calls[method] if @calls.include?(method)
@calls[method] = @target.public_send method, *args, &block
else
::Kernel.raise Error
end
end
end
......

Finished in 0.01118 seconds
6 examples, 0 failures
Деян Хаджиев
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Деян Хаджиев
class Memoizer < BasicObject
def initialize(object)
@instanced_object = object
@returned_values = {}
end
def method_missing(name, *args, &block)
if block
@instanced_object.send(name, *args, &block)
else
@returned_values[name] = @instanced_object.send(name, *args) if @returned_values[name] == nil
@returned_values[name]
end
end
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-17exk7s/spec.rb:48: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.01439 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-17exk7s/spec.rb:35 # Memoizer works with nil return values
Емануела Моллова
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Емануела Моллова
class Memoizer < BasicObject
def initialize(target)
@target = target
@cache = {}
end
def method_missing(method, *args, &block)
if @target.respond_to? method
key = "#{method}, #{args}"
return @cache[key] if @cache.has_key? key
value = @target.public_send method, *args, &block
block.nil? ? @cache[key] = value : value
else
::Kernel.raise ::NoMethodError
end
end
end
......

Finished in 0.01303 seconds
6 examples, 0 failures
Красимира Божанова
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Красимира Божанова
class Memoizer < BasicObject
def initialize(target)
@target = target
@cash = {}
end
def method_missing(method, *args, &block)
if block
@target.public_send method, *args, &block
else
if not @cash[[method, *args]]
@cash[[method, *args].freeze] = @target.public_send method, *args
end
@cash[[method, *args]]
end
end
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-er6xx1/spec.rb:48: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.01128 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-er6xx1/spec.rb:35 # Memoizer works with nil return values
Калоян Калудов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Калоян Калудов
class Memoizer < BasicObject
def initialize(instance)
@instance = instance
@results = {}
@arguments = {}
end
def method_missing(method, *args, &block)
::Kernel.raise ::NoMethodError unless @instance.respond_to? method
return @instance.public_send(method, *args, &block) if block
if @results.include?(method) and @arguments[method] == args
@results[method]
else
@arguments[method] = args
@results[method] = @instance.public_send method, *args
end
end
end
......

Finished in 0.01262 seconds
6 examples, 0 failures
Никола Ненков
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Никола Ненков
class Memoizer < BasicObject
attr_reader :object
attr_reader :cache
def initialize(object)
@object = object
@cache = {}
end
def method_missing(method_name, *args, &block)
if ::Kernel.block_given?
@object.public_send method_name, *args, &block
elsif @cache.include? [method_name, args]
@cache[[method_name, args]]
else
@cache[[method_name, args]] = @object.public_send method_name, *args
end
end
end
......

Finished in 0.01344 seconds
6 examples, 0 failures
Александър Попов
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Александър Попов
class Memoizer < BasicObject
def initialize(target)
@target = target
@calls = {}
end
def method_missing(method, *args, &block)
::Kernel.raise ::NoMethodError unless @target.respond_to? method
if ::Kernel.block_given?
@target.public_send(method, *args, &block)
else
@calls[method] ||= {}
@calls[method][args] ||= @target.public_send(method, *args)
end
end
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-15inmli/spec.rb:48: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.01166 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-15inmli/spec.rb:35 # Memoizer works with nil return values
Мария Митева
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Мария Митева
class Memoizer < BasicObject
def initialize(object)
@instance = object
@cache = {}
end
def method_missing(method, *args, &block)
call_id = method.to_s + args.to_s
unless block
if @cache.has_key?(call_id)
return @cache[call_id]
else
value = call_method method, *args
@cache[call_id] = value
return value
end
else
call_method method, *args, &block
end
end
def call_method(method, *args, &block)
if @instance.respond_to? method
@instance.public_send method, *args, &block
else
::Kernel.raise ::NoMethodError
end
end
end
......

Finished in 0.01151 seconds
6 examples, 0 failures
Росен Рачев
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Росен Рачев
class Memoizer < BasicObject
def initialize(object)
@object = object
@cache = {}
end
def method_missing(name, *arg, &block)
if @cache.include? (name.to_s + ',' + arg.join(','))
return @cache[name.to_s + ',' + arg.join(',')]
end
if ::Kernel.block_given?
@object.public_send name, *arg, &block
else
@cache[name.to_s + ',' + arg.join(',')] = @object.public_send name, *arg
end
end
end
......

Finished in 0.01216 seconds
6 examples, 0 failures
Ангел Цанев
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Ангел Цанев
class Memoizer < BasicObject
attr_reader :hash
def initialize(object)
@object = object
@hash = {}
end
def method_missing(method, *args, &block)
if @object.respond_to? method
if !::Kernel.send:block_given?
if !(@hash.has_key?(method.to_s + '(' + args.join(',') + ')'))
value = @object.public_send method, *args
@hash[method.to_s + '(' + args.join(',') + ')'] = value
return value
else
return @hash[method.to_s + '(' + args.join(',') + ')']
end
else
@object.public_send method, *args, &block
end
else
::Kernel.send:raise,NoМethodError
end
end
end
.F....

Failures:

  1) Memoizer raises NoMethodError when an unknown method is called
     Failure/Error: -> { memoizer.no_such_string_method }.should raise_error NoMethodError
       expected NoMethodError, got #<NameError: uninitialized constant Memoizer::NoМethodError> with backtrace:
         # /tmp/d20131117-20795-1r3hl1j/solution.rb:23:in `method_missing'
         # /tmp/d20131117-20795-1r3hl1j/spec.rb:12:in `block (3 levels) in <top (required)>'
         # /tmp/d20131117-20795-1r3hl1j/spec.rb:12: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/d20131117-20795-1r3hl1j/spec.rb:12: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.01187 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-1r3hl1j/spec.rb:9 # Memoizer raises NoMethodError when an unknown method is called
Георги Пурнаров
  • Некоректно
  • 3 успешни тест(а)
  • 3 неуспешни тест(а)
Георги Пурнаров
class Memoizer
attr_accessor :called_methods
def initialize(object)
@object = object
@called_methods = {}
end
def method_missing(name, *arguments, &block)
return called_methods[name] if called_methods[name]
if @object.respond_to? name
called_methods[name]=@object.send(name, *arguments, &block) unless block_given?
@object.send(name, *arguments, &block)
else
raise super
end
end
end
..FFF.

Failures:

  1) Memoizer memoizes results
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-13adqwg/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)>'

  2) Memoizer returns the actual class of the target object
     Failure/Error: Memoizer.new(Object.new).class.should eq Object
       
       expected: Object
            got: Memoizer
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -Object
       +Memoizer
     # /tmp/d20131117-20795-13adqwg/spec.rb:32: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) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 4
       
       (compared using ==)
     # /tmp/d20131117-20795-13adqwg/spec.rb:48: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.01534 seconds
6 examples, 3 failures

Failed examples:

rspec /tmp/d20131117-20795-13adqwg/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-13adqwg/spec.rb:31 # Memoizer returns the actual class of the target object
rspec /tmp/d20131117-20795-13adqwg/spec.rb:35 # Memoizer works with nil return values
Марио Даскалов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Марио Даскалов
class Memoizer < BasicObject
def initialize(wrapped_object)
@wrapped_object = wrapped_object
@cache = ::Hash.new { |hash, key| hash[key] = {} }
end
def method_missing(method, *args, &block)
if block
@wrapped_object.public_send(method, *args, &block)
else
unless @cache.include? method and @cache[method].include? args
@cache[method][args] = @wrapped_object.public_send(method, *args)
end
@cache[method][args]
end
end
end
......

Finished in 0.01436 seconds
6 examples, 0 failures
Давид Петров
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Давид Петров
class Memoizer < BasicObject
def initialize(target)
@target = target
@calls_hash={}
end
def method_missing(method, *args, &block)
if @target.respond_to? method
if ::Kernel.block_given?
@target.public_send method, *args, &block
elsif @calls_hash.has_key?([method,*args])
@calls_hash[[method,*args]]
else
@calls_hash[[method,*args]] = @target.public_send method, *args,&block
end
else
::Kernel.send :raise, ::NoMethodError
end
end
end
......

Finished in 0.0135 seconds
6 examples, 0 failures
Илия Ватахов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Илия Ватахов
class Cache
def initialize
@data = {}
end
def add(key, value)
@data[key] = value
end
def get(key)
@data[key]
end
def member?(key)
@data.member? key
end
end
class Memoizer < BasicObject
def initialize(object)
@object = object
@cache = ::Cache.new
end
def method_missing(method, *args, &block)
if @object.respond_to? method
call method, *args, &block
else
::Kernel::raise ::NoMethodError
end
end
private
def call(method, *args, &block)
if ::Kernel::block_given?
@object.send method, *args, &block
elsif @cache.member? [method, args]
@cache.get [method, args]
else
@cache.add [method, args], @object.send(method, *args)
end
end
end
......

Finished in 0.01191 seconds
6 examples, 0 failures
Антонио Николов
  • Некоректно
  • 4 успешни тест(а)
  • 2 неуспешни тест(а)
Антонио Николов
class Memoizer < BasicObject
def initialize(object)
@object = object
@called = {}
end
def method_missing(method, *args, &block)
if @object.respond_to? method
return @called[[method, *args]] if @called.has_key?([method, *args]) and block == nil
@called[[method, *args]] = @object.public_send method, *args if block == nil
@object.public_send method, *args, &block
else
::Kernel.raise ::NoMethodError
end
end
end
..F.F.

Failures:

  1) Memoizer memoizes results
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1jwuily/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)>'

  2) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1jwuily/spec.rb:48: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.01142 seconds
6 examples, 2 failures

Failed examples:

rspec /tmp/d20131117-20795-1jwuily/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-1jwuily/spec.rb:35 # Memoizer works with nil return values
Слав Керемидчиев
  • Некоректно
  • 4 успешни тест(а)
  • 2 неуспешни тест(а)
Слав Керемидчиев
class Memoizer < BasicObject
class Error < ::NoMethodError; end
attr_reader :calls
def initialize(target)
@target = target
@calls = {}
end
def method_missing(method, *args, &block)
if @target.respond_to? method
if @calls.has_key? method
@calls[method]
else
if !block
@calls[method] = @target.public_send method, *args, &block
end
@target.public_send method, *args, &block
end
else
::Kernel.raise Error
end
end
end
..F.F.

Failures:

  1) Memoizer memoizes results
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1c672f/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)>'

  2) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1c672f/spec.rb:48: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.01447 seconds
6 examples, 2 failures

Failed examples:

rspec /tmp/d20131117-20795-1c672f/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-1c672f/spec.rb:35 # Memoizer works with nil return values
Иван Капукаранов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Капукаранов
class Memoizer < BasicObject
def initialize(object)
@object = object
@cache = {}
end
def method_missing(name, *args, &block)
if @object.respond_to? name
if ::Object.send :block_given?
@object.public_send name, *args, &block
else
if @cache[name] and @cache[name][1..-1] == args
@cache[name].first
else
cached_value = @object.public_send name, *args
@cache[name] = [cached_value, *args]
@cache[name].first
end
end
else
::Kernel.send :raise, ::NoMethodError
end
end
end
......

Finished in 0.01183 seconds
6 examples, 0 failures
Моника Димитрова
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Моника Димитрова
class Memoizer < BasicObject
def initialize(object)
@object = object
@calls = {}
end
def method_missing(name, *args, &block)
method = name.to_s + args.join()
if @calls.has_key? method
@calls[method]
elsif @object.respond_to? name
result = @object.send name, *args, &block
@calls[method] = result if block.nil?
else
::Kernel.raise ::NoMethodError
end
end
end
......

Finished in 0.01255 seconds
6 examples, 0 failures
Мария Терзиева
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Мария Терзиева
class Memoizer < BasicObject
def initialize(instance)
@instance = instance
@cache = {}
end
def cache(method, *args)
@cache["#{method}, #{args}"] = @instance.public_send method, *args
end
def memoize(method, *args)
@cache["#{method}, #{args}"] || cache(method, *args)
end
def method_missing(method, *args, &block)
if ::Kernel.send :block_given?
return @instance.public_send method, *args, &block
else
memoize method, *args
end
end
private :cache, :memoize
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-vv3mi9/spec.rb:48: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.01516 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-vv3mi9/spec.rb:35 # Memoizer works with nil return values
Диан Николов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Диан Николов
class Memoizer < BasicObject
def initialize(object)
@instance = object
@callsCache = ::Hash.new
end
def method_missing(name, *args, &block)
if (@callsCache.has_key?(name) and @callsCache[name].has_key?(args))
@callsCache[name][args]
else
result = @instance.public_send name, *args, &block
if nil == block
@callsCache[name] = { args => result }
end
result
end
end
def respond_to_missing?(method_name, include_private)
@instance.respond_to? method_name, include_private
end
end
......

Finished in 0.01584 seconds
6 examples, 0 failures
Веселин Генадиев
  • Некоректно
  • 0 успешни тест(а)
  • 6 неуспешни тест(а)
Веселин Генадиев
class Memorizer < BasicObject
attr_reader :calls
def initialize(instance)
@instance = instance
@calls = {}
end
def method_missing(method, *args, &block)
if @calls.has_key? method
@calls[method]
elsif @instance.respond_to?(method) && !block
@calls[method] = @instance.public_send method, *args
@calls[method]
else
@instance.public_send method, *args, &block
end
end
end
FFFFFF

Failures:

  1) Memoizer calls the methods of the target class
     Failure/Error: memoizer = Memoizer.new string
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/spec.rb:4: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) Memoizer raises NoMethodError when an unknown method is called
     Failure/Error: memoizer = Memoizer.new "A string"
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/spec.rb:10: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) Memoizer memoizes results
     Failure/Error: memoized = Memoizer.new object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/spec.rb:24: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) Memoizer returns the actual class of the target object
     Failure/Error: Memoizer.new(Object.new).class.should eq Object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/spec.rb:32: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) Memoizer works with nil return values
     Failure/Error: memoized = Memoizer.new object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/spec.rb:44: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) Memoizer works with blocks
     Failure/Error: memoized = Memoizer.new array
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-1cw0ly9/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)>'

Finished in 0.01756 seconds
6 examples, 6 failures

Failed examples:

rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:2 # Memoizer calls the methods of the target class
rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:9 # Memoizer raises NoMethodError when an unknown method is called
rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:31 # Memoizer returns the actual class of the target object
rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:35 # Memoizer works with nil return values
rspec /tmp/d20131117-20795-1cw0ly9/spec.rb:51 # Memoizer works with blocks
Венцислав Велков
  • Некоректно
  • 4 успешни тест(а)
  • 2 неуспешни тест(а)
Венцислав Велков
class Memoizer < BasicObject
attr_reader :cache
def initialize(target)
@target = target
@cache = {}
end
def method_missing(method, *args, &block)
if @target.respond_to? method
if block
@target.public_send method, *args, &block
else
if @cache.has_key?([method, *args])
@cache.fetch([method, *args])
else
@cache[[method, *args]] = @target.public_send method, *args
@target.public_send method, *args, &block
end
end
else
::Kernel.raise ::NoMethodError
end
end
end
..F.F.

Failures:

  1) Memoizer memoizes results
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-lkxu0v/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)>'

  2) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-lkxu0v/spec.rb:48: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.01264 seconds
6 examples, 2 failures

Failed examples:

rspec /tmp/d20131117-20795-lkxu0v/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-lkxu0v/spec.rb:35 # Memoizer works with nil return values
Валентин Ейткен
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Валентин Ейткен
class Memoizer < BasicObject
def initialize(instance)
@instance = instance
@cache = {}
end
def method_missing(name, *args, &block)
if @instance.respond_to? name
if block
@instance.send(name, *args, &block)
elsif cached?(name, args)
@cache[[name, args]]
else
@cache[[name, args]] = @instance.send(name, *args)
end
else
super
raise ::NoMethodError
end
end
def respond_to_missing?(name, include_private = false)
@instance.respond_to?(name) || super
end
private
def cached?(name, args)
@cache[[name, args]]
end
end
....F.

Failures:

  1) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-jxvwxd/spec.rb:48: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.01426 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-jxvwxd/spec.rb:35 # Memoizer works with nil return values
Иван Латунов
  • Некоректно
  • 0 успешни тест(а)
  • 6 неуспешни тест(а)
Иван Латунов
class Memorizer < BasicObject
def initialize(object)
@object = object
@cache = {}
end
def method_missing(method, *args, &block)
if @object.respond_to? method
if block_given?
@object.send method, *args, &block
else
method_key = [method, *args]
if @cache.include? method_key
@cache[method_key]
else
@cache[method_key] = @object.send method, *args
@cache[method_key]
end
end
else
raise ::Kernel.raise ::NoMethodError
end
end
def inspect
@object.inspect
end
end
FFFFFF

Failures:

  1) Memoizer calls the methods of the target class
     Failure/Error: memoizer = Memoizer.new string
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/spec.rb:4: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) Memoizer raises NoMethodError when an unknown method is called
     Failure/Error: memoizer = Memoizer.new "A string"
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/spec.rb:10: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) Memoizer memoizes results
     Failure/Error: memoized = Memoizer.new object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/spec.rb:24: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) Memoizer returns the actual class of the target object
     Failure/Error: Memoizer.new(Object.new).class.should eq Object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/spec.rb:32: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) Memoizer works with nil return values
     Failure/Error: memoized = Memoizer.new object
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/spec.rb:44: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) Memoizer works with blocks
     Failure/Error: memoized = Memoizer.new array
     NameError:
       uninitialized constant Memoizer
     # /tmp/d20131117-20795-3qq9pr/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)>'

Finished in 0.01376 seconds
6 examples, 6 failures

Failed examples:

rspec /tmp/d20131117-20795-3qq9pr/spec.rb:2 # Memoizer calls the methods of the target class
rspec /tmp/d20131117-20795-3qq9pr/spec.rb:9 # Memoizer raises NoMethodError when an unknown method is called
rspec /tmp/d20131117-20795-3qq9pr/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-3qq9pr/spec.rb:31 # Memoizer returns the actual class of the target object
rspec /tmp/d20131117-20795-3qq9pr/spec.rb:35 # Memoizer works with nil return values
rspec /tmp/d20131117-20795-3qq9pr/spec.rb:51 # Memoizer works with blocks
Николай Хубанов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Хубанов
class Memoizer < BasicObject
def initialize(object)
@object = object
@cache = {}
end
def method_missing(method, *args, &block)
return @object.send method, *args, &block if ::Kernel.send :block_given?
if !@cache.key? [method, args]
@cache[[method, args]] = @object.send method, *args
end
@cache[[method, args]]
end
end
......

Finished in 0.01388 seconds
6 examples, 0 failures
Петър Мазълов
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Мазълов
class Memoizer < BasicObject
def initialize(instance)
@instance = instance
@cache = {}
end
def method_missing(name, *args, &block)
if block
@instance.send(name, *args, &block)
elsif @cache.has_key? name
@cache[name]
else
@cache[name] = @instance.send(name, *args)
end
end
end
......

Finished in 0.01422 seconds
6 examples, 0 failures
Ангел Венчев
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Венчев
class Memoizer < BasicObject
attr_reader :object
def initialize(object)
@object = object
@cache = {}
end
def method_missing(method_name, *args, &block)
if(@object.respond_to? method_name)
if @cache.member? [method_name,args]
::Kernel.puts " #{ method_name } from cache"
@cache[[method_name,args]]
else
::Kernel.puts " #{ method_name } from method"
method_result = @object.public_send method_name, *args, &block
unless block
@cache[[method_name,args]] = method_result
end
end
else
super
end
end
end
length from method
.. foo from method
 foo from cache
. class from method
. foo from method
 foo from cache
. each from method
 each from method
.

Finished in 0.01473 seconds
6 examples, 0 failures
Кристиян Кисимов
  • Некоректно
  • 4 успешни тест(а)
  • 2 неуспешни тест(а)
Кристиян Кисимов
class Memoizer < BasicObject
attr_reader :calls
def initialize(object)
@object = object
@calls = {}
end
def method_missing(method, *args, &block)
if @object.respond_to? method
if @calls.has_key? method
@calls[method]
else
unless ::Kernel.block_given?
@calls[method] = @object.public_send(method, *args, &block)
end
@object.public_send(method, *args, &block)
end
else
::Kernel.raise ::NoMethodError
end
end
end
..F.F.

Failures:

  1) Memoizer memoizes results
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1c9c4gl/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)>'

  2) Memoizer works with nil return values
     Failure/Error: call_count.should eq 1
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20131117-20795-1c9c4gl/spec.rb:48: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.01218 seconds
6 examples, 2 failures

Failed examples:

rspec /tmp/d20131117-20795-1c9c4gl/spec.rb:15 # Memoizer memoizes results
rspec /tmp/d20131117-20795-1c9c4gl/spec.rb:35 # Memoizer works with nil return values
Борислава Аладжова
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Борислава Аладжова
class Memoizer < BasicObject
attr_reader :method_calls
def initialize(object)
@object = object
@method_calls = {}
end
def method_missing(method, *args, &block)
if @object.respond_to? method
return @object.public_send(method, *args, &block) if ::Kernel.block_given?
if @method_calls.has_key? method
@object.public_send(method, *args) if method.to_s.end_with? '!'
else
@method_calls[method] = @object.public_send(method, *args)
end
@method_calls[method]
else
::Kernel.send :raise, ::NoMethodError
end
end
end
......

Finished in 0.01095 seconds
6 examples, 0 failures
Ясен Трифонов
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Ясен Трифонов
class Memoizer < BasicObject
attr_reader :target, :calls
def initialize(target)
@target = target
@calls = ::Hash.new { |h, k| h[k] = {} }
end
def method_missing(method, *args, &block)
if @target.respond_to? method
if ::Kernel.block_given?
return @target.public_send method, *args, &block
end
if @calls.include? method
if @calls[method].include? args
return @calls[method][args]
end
end
# ::Kernel.puts "calling method"
@calls[method][args] = @target.public_send method, *args, &block
else
::Kernel.raise NoMethodError
end
end
end
.F....

Failures:

  1) Memoizer raises NoMethodError when an unknown method is called
     Failure/Error: -> { memoizer.no_such_string_method }.should raise_error NoMethodError
       expected NoMethodError, got #<NameError: uninitialized constant Memoizer::NoMethodError> with backtrace:
         # /tmp/d20131117-20795-1lhx3ms/solution.rb:23:in `method_missing'
         # /tmp/d20131117-20795-1lhx3ms/spec.rb:12:in `block (3 levels) in <top (required)>'
         # /tmp/d20131117-20795-1lhx3ms/spec.rb:12: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/d20131117-20795-1lhx3ms/spec.rb:12: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.01455 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-1lhx3ms/spec.rb:9 # Memoizer raises NoMethodError when an unknown method is called
Христо Хърсев
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Христо Хърсев
class Memoizer
def initialize(instance)
@instance = instance
@cached = {}
end
protected
def method_missing(method, *args, &block)
return @instance.public_send method, *args, &block if block
unless @cached.keys.include?(method.to_sym)
@cached[method.to_sym] = @instance.public_send method, *args
end
@cached[method.to_sym]
end
def respond_to_missing?(method, *)
begin
@instance.public_send method
rescue NoMethodError
return false
end
true
end
end
...F..

Failures:

  1) Memoizer returns the actual class of the target object
     Failure/Error: Memoizer.new(Object.new).class.should eq Object
       
       expected: Object
            got: Memoizer
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -Object
       +Memoizer
     # /tmp/d20131117-20795-1qv7cvq/spec.rb:32: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.01462 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-1qv7cvq/spec.rb:31 # Memoizer returns the actual class of the target object
Илиян Бобев
  • Коректно
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Бобев
class Memoizer < BasicObject
def initialize source
@source = source
@cache = {}
end
def method_missing name, *args, &block
::Kernel.raise ::NoMethodError unless @source.respond_to? name
if block
return @source.public_send name, *args, &block
else
if @cache.include? (args + [name])
@cache[args + [name]]
else
@cache[args + [name]] = @source.send name, *args
end
end
end
end
......

Finished in 0.01105 seconds
6 examples, 0 failures
Наталия Пацовска
  • Некоректно
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)
Наталия Пацовска
class Memoizer
class Error < ::NoMethodError
end
def initialize(instance)
@cached_responses = {}
@instance = instance
end
def method_missing(method, *args, &block)
if block.class == ::Proc
execute_method method, *args, &block
else
cache_method method, *args
end
end
private
def execute_method(method, *args, &block)
if @instance.respond_to? method
@instance.public_send method, *args, &block
else
::Kernel.send :raise, Error
end
end
def cache_method(method, *args)
if @cached_responses.key?([:method, args])
@cached_responses[[:method, args]]
else
@cached_responses[[:method, args]] = execute_method method, *args
end
end
end
...F..

Failures:

  1) Memoizer returns the actual class of the target object
     Failure/Error: Memoizer.new(Object.new).class.should eq Object
       
       expected: Object
            got: Memoizer
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -Object
       +Memoizer
     # /tmp/d20131117-20795-pi0imb/spec.rb:32: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.016 seconds
6 examples, 1 failure

Failed examples:

rspec /tmp/d20131117-20795-pi0imb/spec.rb:31 # Memoizer returns the actual class of the target object