Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Latest commit

 

History

History
65 lines (49 loc) · 2.2 KB

README.md

File metadata and controls

65 lines (49 loc) · 2.2 KB

RubyUnderscore

Closures are very useful tools, and ruby Enumerable mixin makes them even more useful.

However, as you decompose more and more your iterations into a sequence of maps, selects, rejects, group_bys and reduces, more commonly you see simple blocks such as:

dates.select { |d| d.greater_than(old_date) }
collection.map { |x| x.invoke }
classes.reject { |c| c.subclasses.include?(Array) }

RubyUnderscore modify classes so that you can also use a short notation for simple closures. With such, the above examples can be written as:

dates.select _.greater_than old_date
collection.map _.invoke
classes.reject _.subclasses.include? Array

Just replace the iterating argument with the underscore symbol (_), and ditch the parenthesis. More info

Quick Example

The example consists of getting all instance methods of String, Array, Class that end with 'd?'

require 'ruby_underscore'

class MethodFinder
  include RubyUnderscore::Base

  def find_interrogation_methods
    [String, Array, Class].map(_.public_instance_methods.grep /d\?$/).flatten.sort.uniq
  end
end
p MethodFinder.new.find_interrogation_methods

Using Ruby Underscore

As in the example above, simply by including the module include RubyUnderscore::Base on the class, all methods (class methods as well) will allow you to use the underscore symbol to write simple blocks.

Meta

Created by Daniel Ribeiro

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

http://github.com/danielribeiro/RubyUnderscore