Skip to content

Instantly share code, notes, and snippets.

@guiferrpereira
Forked from peterc/methods_returning.rb
Created January 30, 2014 18:21
Show Gist options
  • Save guiferrpereira/8715229 to your computer and use it in GitHub Desktop.
Save guiferrpereira/8715229 to your computer and use it in GitHub Desktop.
require 'stringio'
require 'timeout'
class Object
def methods_returning(expected, *args, &blk)
old_stdout = $>
$> = StringIO.new
methods.select do |meth|
Timeout::timeout(1) { dup.public_send(meth, *args, &blk) == expected rescue false } rescue false
end
ensure
$> = old_stdout
end
end
# ----------------
p [1, nil, 'x'].methods_returning [1, 'x']
# => [:compact, :compact!]
p [1, 2, 3].methods_returning [3, 2, 1]
# => [:reverse, :reverse!]
p [1, 2, 3].methods_returning []
# => [:values_at, :clear, :singleton_methods, :protected_methods, :instance_variables]
p [1, 2, 3].methods_returning([2, 4, 6]) { |i| i * 2 }
# => [:collect, :collect!, :map, :map!, :flat_map, :collect_concat]
p [1, 2, 3].methods_returning('123')
# => [:join]
p (1..5).methods_returning([[1, 3, 5], [2, 4]], &:odd?)
# => [:partition]
p 'A'.methods_returning 'a'
# => [:downcase, :swapcase, :downcase!, :swapcase!]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment