Archive for the 'Ruby' Category

Extracting date and time from text using Ruby

Problem: We want to extract a date and time of the format YYYY-MM-DD MM:SS from a body of text using Ruby. Solution: This is extremely easy in ruby, and other languages that features regular expressions. require “Date” text = “Some text containing a date and a time, for instance 2010-03-19 17:25.” if text =~ /(\d{4})-(\d{2})-(\d{2}) […]

Rounding off floating point numbers in Ruby

Problem: If you want to round a floating point number off to two decimals, there is no standard method in Ruby that does it for you. Solution: The general solution is (f * 10**d).round.to_f / 10**d where f is the floating number and d is the number of decimals. See my code sample for adding […]