Created
February 15, 2012 14:33
-
-
Save masassiez/1836214 to your computer and use it in GitHub Desktop.
Rubyでselfを返すメソッドってあったっけ? ref: http://qiita.com/items/2524
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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