0.0
No commit activity in last 3 years
No release in over 3 years
Adds common extensions to core Ruby classes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.6
 Project Readme

GEM: iron-extensions¶ ↑

Written by Rob Morris @ Irongaze Consulting LLC (irongaze.com)

DESCRIPTION¶ ↑

Helpful extensions to core Ruby classes, plus a little sugar for common patterns. For users of version 1.1.x and lower, please note that as of version 1.2.0 the DSL-specific extensions have been moved to the iron-dsl gem!

ADDED EXTENSIONS¶ ↑

  • Array#list_join - join as a list, ignoring blank/nil entries

    [1, 2, nil, '', 'count'].list_join # => '1, 2, count'
    
  • Enumerable#convert_to_hash - convert an array or other enumerable to a hash using a block or constant

    [:frog, :pig].convert_to_hash {|n| n.to_s.capitalize} # => {:frog => 'Frog', :pig => 'Pig'}
    [:frog, :pig].convert_to_hash(nil) # => {:frog => nil, :pig => nil}
    
  • Enumerable#delete_unless - equivalent to delete_if but with inverted test

    [1,2,3,4,5].delete_unless(&:odd?) # => [1,3,5]
    
  • Class#inspect_only - override #inspect to only show the specified variables or methods

    class User
      # Limit inspect to these attrs
      inspect_only :name, :admin?
    
      def initialize
        @name = 'Bob'
        @password = 'topSECRET!'
        @something_complex = ValidationEngine.new
      end
    
      def admin?
        @name == 'Admin'
      end
    end
    
    # Inspecting instances of this class show only what we need
    User.new.inspect  # => "<User:1892377440 @name='Bob', :admin?=false>"
    
  • File.safe_replace - atomic replacement of a file given a block to generate it

    # Defers deleting old file until block completes successfully (ie no exceptions), then
    # moves the new file into the old file's location
    File.safe_replace('./config') do |file|
      file.write("PRODUCTION: true")
    end
    
  • Fixnum#to_human_size - size to MB/GB/whatever, adapted from Rails

    123456.to_human_size # => "120.5 KB"
    
  • Kernel#capture_stdout - capture all text sent to STDOUT within the passed block

    # Will result in out == 'Hi mom!\n'
    out = capture_stdout do 
      puts 'Hi mom!'
    end
    
  • Math.max/Math.min - find the max/min value passed

    Math.max(2,10) # => 10
    
  • Math.scale_to_fit - shrink width and height vals to fit in a box, preserving aspect ratio

    Math.scale_to_fit(100, 100, 50, 80) # => 50, 50
    
  • Nil#blank? - always true

  • Numeric#to_display - pretty display of numbers with options for number of decimal places and inclusion of thousands separator (US-only, sorry!)

    5000.to_display          # => 5,000
    100.to_display(2)        # => 100.00
    2105.2348.to_display(2)  # => 2,105.23
    
  • Numeric#bound - bound a given number to a range

    4.bound(5,10) # => 5
    
  • Object#in? - sugar to make expressing inclusion clearer

    1.in? [2,3,4] # => false
    'foo'.in? ['foo', 'bar'] # => true
    
  • Range#bound - ensure a number is in a range

    (1..5).bound(6) # => 5
    
  • Regexp::IP_ADDRESS, Regexp::EMAIL_ADDRESS, Regexp::DOMAIN - commonly useful regexen

  • String#blank? - true if empty?

  • String#append / String#prepend - these should be in Ruby core

  • String#to_date - “better” date parser than Date.parse (US-centric)

  • String#to_dashcase - perfect for permalinks!

  • String#smart_truncate - truncate honoring word boundaries

  • String#integer? - true when string represents an integer

  • String#extract - simplifies pulling info out of strings using regexen

    "Irongaze Consulting".extract(/^[a-z]/i) # => "Irongaze"
    dollars, cents = "$12.95".extract(/([0-9]+)\.([0-9]+)/) # => dollars == '12' and cents == '95'
    
  • Symbol#blank? - always false

  • Symbol#to_dashcase - same as for String

SYNOPSIS¶ ↑

To use:

require 'iron-extensions'

After that, simply write code to make use of the new extensions and helper classes.

REQUIREMENTS¶ ↑

  • Ruby 1.9.2 or later

INSTALL¶ ↑

To install, simply run:

sudo gem install iron-extensions

RVM users should drop the ‘sudo’:

gem install iron-extensions