Skip to content

Instantly share code, notes, and snippets.

@masassiez
Created February 15, 2012 14:33
Show Gist options
  • Save masassiez/1836214 to your computer and use it in GitHub Desktop.
Save masassiez/1836214 to your computer and use it in GitHub Desktop.
Rubyでselfを返すメソッドってあったっけ? ref: http://qiita.com/items/2524
class Object
def tap
block_given? ? super : self
end
end
class Array
def dupli
self.group_by(&:tap).reject{ |k, v| v.one? }.keys
end
end
ary = [*0.step(8, 2)] + [*0..9] # => [0, 2, 4, 6, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.dupli # => [0, 2, 4, 6, 8]
class Array
def duplicate
self.group_by{ |e| e }.reject{ |k, v| v.one? }.keys
end
end
ary = [*0.step(8, 2)] + [*0..9] # => [0, 2, 4, 6, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.duplicate # => [0, 2, 4, 6, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment