Skip to content

Instantly share code, notes, and snippets.

@kyanny
Forked from fujimura/pipe.rb
Last active December 18, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyanny/5816029 to your computer and use it in GitHub Desktop.
Save kyanny/5816029 to your computer and use it in GitHub Desktop.
class Object
def pipe
yield self
end
end
# From https://github.com/rspec/rspec-rails/pull/766/files
# Without Object#pipe
types = begin
dirs = Dir['./spec/**/*_spec.rb'].
map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
uniq.
select { |f| File.directory?(f) }
Hash[dirs.map { |d| [d.split('/').last, d] }]
end
# With Object#pipe
types = Dir['./spec/**/*_spec.rb'].
map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
uniq.
select { |f| File.directory?(f) }.
map { |d| [d.split('/').last, d] }.
pipe { |type_and_dirs| Hash[type_and_dirs] }
# With Enumerable#inject
types = Dir['./spec/**/*_spec.rb'].
map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
uniq.
select { |f| File.directory?(f) }.
map { |d| [d.split('/').last, d] }.
inject({}){ |h, type_and_dirs| h.merge! Hash[*type_and_dirs]; h }
#p types
# with_object
types = Dir['./spec/**/*_spec.rb'].
map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
uniq.
select { |f| File.directory?(f) }.
map { |d| [d.split('/').last, d] }.
to_enum.
with_object({}){ |type_and_dirs, h| h.merge! Hash[*type_and_dirs] }
p types
types = Dir['./spec/**/*_spec.rb'].
map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
uniq.
select { |f| File.directory?(f) }.
map { |d| [d.split('/').last, d] }.
tap { |type_and_dirs| break Hash[*type_and_dirs.flatten] }
p types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment