3 New Date and Time Libraries for Rubyists »
Created at: 05.05.2010 17:30, source: Ruby Inside, tagged: Compilation Posts Cool Ruby Tricks Tools
In the UK there's a cliché that goes: "You wait hours for a bus, and then three come along at once!" So it went with these three Ruby date and time libraries. They all made an appearance on RubyFlow last week and are all useful in their own ways, depending on how you're working with dates and times.
ice_cube - Fast querying and expansion of event recurrence rules
ice_cube is a library by John Crepezzi that provides "fast querying and expansion of recurrence rules in Ruby." What this means is that you can create schedules powered by date recurrence rules that can be quite complex (e.g. every 4 years on a Tuesday in the first week of November). Rules like these are defined by chaining methods together, rather than using natural language.
To install:
gem install ice_cube
To use:
require 'ice_cube'
rule = IceCube::Rule.yearly(4).month_of_year(:november).day(:tuesday).day_of_month(2, 3, 4, 5, 6, 7, 8)
schedule = IceCube::Schedule.new(Time.now)
schedule.add_recurrence_rule rule
schedule.first(3)
# => [Tue Nov 02 05:04:38 +0000 2010, Tue Nov 04 05:04:38 +0000 2014, Tue Nov 06 05:04:38 +0000 2018]
ice_cube also supports exporting rules into iCal and YAML formats as well as a natural language equivalent.
John has put together a PDF presentation that shows off more usage, and there are some simple examples on the official site too.
tickle - A natural language parser for recurring events
tickle is a natural language parser for recurring events by Joshua Lippiner that stands in contrast to ice_cube's method driven approach. It depends on the popular chronic natural language date parser and appears (through my experience) to be for Ruby 1.9+ only.
tickle lets you throw it things like every 4 days starting next saturday, every other week, the tenth of the month and similar (there are a lot of examples on tickle's GitHub page). You pass these to the Tickle.parse method and you get the next occurrence of the rule.
To install:
gem install tickle
To use:
require 'tickle'
Tickle.parse('every 4 days starting next saturday')
# => 2010-05-01 12:00:00 +0000
tickle isn't particularly mature yet and it only makes it easy to get the next occurrence of your rule, but the developer suggests that once an event has occurred, you automatically run Tickle again to get the next date. In this way, it seems tickle is well suited for situations where only the next occurrence needs to be stored and the rule can be kept in a separate database column or similar.
business_time - Time and date offsets based on "business time/hours"
business_time is a new library that works with the concept of "business time" or "business hours." Rather than just letting you perform operations on dates by absolute numbers of days or hours, you can now work with business days and hours of your own definition. business_time depends heavily on Active Support.
To install:
gem install business_time
Note: business_time depends on Active Support (gem: activesupport)
To use:
require 'active_support'
require 'business_time'
# Examples "from now"
4.business_hours.from_now
5.business_days.from_now
# Using user supplied dates
my_birthday = Date.parse("August 4th, 2010")
10.business_days.before(my_birthday)
# Add a day to not count as a business day
BusinessTime::Config.holidays << my_birthday
# Overlapping days are OK
6.business_hours.after(Time.parse("August 3rd, 3:00pm"))
more »
Building A Well Formed Number Handling Class From Scratch »
Created at: 25.02.2010 03:21, source: Ruby Inside, tagged: Ruby Tricks Tutorials
Over on the Ruby Best Practices blog,
Robert Klemme walks through the process of building a new numeric class from scratch in Ruby - taking into account all the gotchas and considerations that pop up along the way. Robert's task is harder and more involved than you'd initially suspect.!
Robert chooses to build a HexNum class that can represent integers that are then displayed as hex numbers. There are considerations to be made with handling conversions from existing numeric types and his new HexNum class, conversions to other types, supporting standard comparison methods, and overloading.
All of the above concerns are covered in the post with all the quality and detail you should expect from the RBP blog by now. This post in particular should prove interesting to most Ruby developers who feel happy digging deep (no, this isn't an article for beginners!).
[job] BeenVerified.com is currently looking for a Ruby and Rails developer to join their team in New York City. Alternatively, check out one of the other 12 jobs on our Ruby jobs board!
more »
Ruby Quicktips: A Tumblelog of Quick Ruby Tips »
Created at: 13.02.2010 01:31, source: Ruby Inside, tagged: enterprise Ruby Tricks
Ruby Quicktips is a Tumblr-powered tumblelog (think of a blog but in bite-sized chunks) by Daniel Pietzsch that presents a growing array of Ruby related tips and interesting code snippets. Daniel seems keen for you to contribute, and you can do so on its submissions page. There's an "about us" post if you want to learn more in general.
Daniel has done a good job of curating the 20 or so tips that he's put up over the last couple of months, and I bet with your help, it could become a significant resource. Picking up a few tips each week is a great way to keep your mind fresh and the ideas flowing! The tips posted so far are mostly quite basic, but with more submissions, a wider gamut of skill levels could be covered.
A selection of the tips posted so far:
- Retrieve the last return value with underscore (_)
- Using the "try" method
- Date manipulation
- Disabling input fields on a form based on a condition (Rails)
- all? and any?
Ruby Quicktips also has a Twitter account you can follow to be notified when the latest tip has been published. There's also an RSS feed if you prefer that.
more »
