I defined this method:
# @return [Array<String>] items
def produce_items
["foo", "bar", "baz"]
end
A correct usage would be, obviously,
produce_items.each do |i|
puts i
end
But I wrote this, which silently does nothing:
produce_items do |i|
puts i
end
Is there a way to declare produce_items
in such a way that my incorrect usage produces an error/exception? If MRI cannot do it, can an alternative interpreter do it? Can a static analysis tool like RuboCop or ruby-lint do it?
I suspect it may be hard because there are common idioms where methods take an optional block:
def block_optional_implicitly
if block_given?
puts "Got a block"
yield
else
puts "Did not get a block"
end
end
def block_optional_explicitly(&block)
unless block.nil?
puts "Got a block"
block.call
else
puts "Did not get a block"
end
end
(This is a reverse of the question How to require a block in Ruby?)