Skip to content

One million code monkeys, all at their keyboards, writing Ruby.

License

Notifications You must be signed in to change notification settings

cgore/ruby-monkey-patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The monkey-patch Ruby Library

One of the best, and worst, features of Ruby is “monkey-patching”, extending the language’s built-in classes and modules to suit your desires. This is my library for that.

Installation

gem install monkey-patch

Usage

require 'monkey-patch'

Extensions to the Pathname class

The =~ method

I often want to match file path names with regular expressions. Calling to_s first kind of irritating.

p = Pathname.new("/home/chris/.emacs.d/init.el")
p =~ /$.*\.el^/ # Matches.

Extensions to the String class

The camelcase_to_snakecase method

De-uglifies a variable name.

"lookMaICodeInJava".camelcase_to_snakecase # returns "look_ma_i_code_in_java"

The valid_json? method

Returns true if the string is parsable by JSON.parse, false otherwise.

"blarg".valid_json? # Returns false.
'{"foo": "bar"}'.valid_json? # Returns true.

Extensions to the Time class

Date builders: commercial, england, gregorian, italy, jd, jisx0301, julian, ld, mjd, ordinal, today, yesterday

For each of the above methods that construct a Date class, these will then immediately convert it into a Time. Be aware, it will use your current time zone in a lot of situations.

Time.commercial(2001,5,6) # Returns the Time: 2001-02-03 00:00:00 -0600

start_of_second, start_of_minute, start_of_half_hour, start_of_hour, start_of_half_day, start_of_day, start_of_week, start_of_month, start_of_year, start_of_decade, start_of_full_decade, start_of_century, start_of_full_century

These will all return new Time instances relative to the specified time, in the same time zone.

t = Time.new "2014-03-20 13:44:33 -0600"
t.start_of_minute # Returns the time object for 2014-03-20 13:44:00 -0600
t.start_of_half_hour # Returns the time object for 2014-03-20 13:30:00 -0600
t.start_of_hour # Returns the time object for 2014-03-20 13:00:00 -0600
t.start_of_half_day # Returns the time object for 2014-03-20 12:00:00 -0600
t.start_of_day # Returns the time object for 2014-03-20 00:00:00 -0600
# Sunday is the first day of the week, and any ISO standards that try to tell
# you otherwise are written by idiots and liars.
t.start_of_week # Returns the time object for 2014-03-17 00:00:00 -0600
t.start_of_month # Returns the time object for 2014-03-01 00:00:00 -0600
t.start_of_year # Returns the time object for 2014-01-01 00:00:00 -0600
t.start_of_decade # Returns the time object for 2010-01-01 00:00:00 -0600
t.start_of_full_decade # Returns the time object for 2011-01-01 00:00:00 -0600
t.start_of_century # Returns the time object for 2000-01-01 00:00:00 -0600
t.start_of_full_century # Returns the time object for 2001-01-01 00:00:00 -0600

About

One million code monkeys, all at their keyboards, writing Ruby.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages