Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created November 15, 2011 06:44
Show Gist options
  • Select an option

  • Save pasberth/1366340 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1366340 to your computer and use it in GitHub Desktop.
Array#=~ メソッドはこういう挙動であるべき的な
class Object
def in? enum
enum.include? self
end
end
module Enumerable
def =~ _
if _.kind_of? self.class
duplicate _
else
include? _
end
end
def duplicate other
i = 0
each do |mine|
if mine == other.first
matched = true
other.zip(last count - i) do |theirs, matchers|
break matched = false unless theirs == matchers
end
return i if matched
end
i += 1
end
nil
end
end
puts [1, 2, 3] =~ [1, 2] # => 0
puts [1, 2, 3] =~ [2, 3] # => 1
puts [1, 2, 3] =~ [1, 3] # => nil
puts [1, 2, 3] =~ 1 # => true # indexと同じのほうがいいかも
puts [1, 2, 3] =~ 0 # => false # nil のがいいかも
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment