Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 28, 2014 06:06
Show Gist options
  • Save JoshCheek/5a1e3cd31088a0e46dc2 to your computer and use it in GitHub Desktop.
Save JoshCheek/5a1e3cd31088a0e46dc2 to your computer and use it in GitHub Desktop.
Goodies are at the bottom.
class Symbol
def enter(rider)
(!self).call rider
end
def !@
-> subject do
method = subject.method(self)
return method.call if method.arity.zero?
method.to_proc.curry
end
end
end
class Proc
def enter(rider)
call(rider)
end
end
class Array
def enter(rider)
(method_name, *args) = self
block = args.pop if args.last.kind_of?(Proc) || args.last.kind_of?(Method)
rider.__send__ method_name, *args, &block
end
def !@
-> subject do
subject.__send__ *self
end
end
remove_method :|
end
class Fixnum
remove_method :|
end
class Object
def ^(rider)
rider | self
end
def |(wave)
wave.enter self
end
end
"abc" | :upcase
# => "ABC"
[:a, /b/, 'c'] | !:* ^ 2
# => [:a, /b/, "c", :a, /b/, "c"]
2 | :* ^ 3
# => 6
ENV | :keys | :grep ^ /ruby|gem/i
# => ["GEM_HOME", "TM_RUBY", "RUBY_ENGINE", "RUBY_PATCHLEVEL", "GEM_ROOT", "GEM_PATH", "RUBY_ROOT", "RUBYOPT", "RUBYLIB", "RUBY_VERSION"]
[*'a'..'z'] | # => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
[:each_slice, 2] | # => #<Enumerator: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]:each_slice(2)>
[:map, ![:join, '']] | # => ["ab", "cd", "ef", "gh", "ij", "kl", "mn", "op", "qr", "st", "uv", "wx", "yz"]
[:each_slice, 2] | # => #<Enumerator: ["ab", "cd", "ef", "gh", "ij", "kl", "mn", "op", "qr", "st", "uv", "wx", "yz"]:each_slice(2)>
[:map, ![:join, '|']] | # => ["ab|cd", "ef|gh", "ij|kl", "mn|op", "qr|st", "uv|wx", "yz"]
[:map, ![:insert, 0, ')']] | # => [")ab|cd", ")ef|gh", ")ij|kl", ")mn|op", ")qr|st", ")uv|wx", ")yz"]
[:map, ![:insert, -1, '(']] | # => [")ab|cd(", ")ef|gh(", ")ij|kl(", ")mn|op(", ")qr|st(", ")uv|wx(", ")yz("]
[:join, '-'] | # => ")ab|cd(-)ef|gh(-)ij|kl(-)mn|op(-)qr|st(-)uv|wx(-)yz("
:reverse # => "(zy)-(xw|vu)-(ts|rq)-(po|nm)-(lk|ji)-(hg|fe)-(dc|ba)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment