<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>solutions.hans-eric.com &#187; Ruby</title>
	<atom:link href="http://solutions.hans-eric.com/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://solutions.hans-eric.com</link>
	<description>Hans-Eric Grönlund's Log of Solutions to Technical Problems</description>
	<lastBuildDate>Thu, 22 Dec 2011 15:42:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extracting date and time from text using Ruby</title>
		<link>http://solutions.hans-eric.com/extracting-date-and-time-from-text-using-ruby</link>
		<comments>http://solutions.hans-eric.com/extracting-date-and-time-from-text-using-ruby#comments</comments>
		<pubDate>Fri, 19 Mar 2010 16:07:31 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Regular expressions]]></category>
		<category><![CDATA[string handling]]></category>

		<guid isPermaLink="false">http://solutions.hans-eric.com/?p=37</guid>
		<description><![CDATA[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}) (\d{2}):(\d{2})/
  dt = DateTime.new($1.to_i, $2.to_i, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong><br />
We want to <em>extract a date and time</em> of the format YYYY-MM-DD MM:SS from a body of text using Ruby.</p>
<p><strong>Solution:</strong><br />
This is extremely easy in ruby, and other languages that features regular expressions.</p>
<pre><code>
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}) (\d{2}):(\d{2})/
  dt = DateTime.new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i)
end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://solutions.hans-eric.com/extracting-date-and-time-from-text-using-ruby/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rounding off floating point numbers in Ruby</title>
		<link>http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby</link>
		<comments>http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby#comments</comments>
		<pubDate>Tue, 09 Jun 2009 15:15:11 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://solutions.hans-eric.com/?p=8</guid>
		<description><![CDATA[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 this functionality to the Float class.
Although [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem:</h4>
<p>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.</p>
<h4>Solution:</h4>
<p>The general solution is</p>
<pre><code>(f * 10**d).round.to_f / 10**d</code></pre>
<p>where f is the floating number and d is the number of decimals.</p>
<p>See <a title="Floating Point Round off In Ruby" href="http://www.hans-eric.com/code-samples/ruby-floating-point-round-off/">my code sample for adding this functionality to the Float class</a>.</p>
<p>Although the above works you should concider the reason you&#8217;re performing the rounding (or equivalent) operation. If it&#8217;s for presentation reasons only a better way might be to use a format string instead, and leave the original data intact.<br />
Here&#8217;s an example where a representation (rounded_str) of a floating point number (f) is rounded to two decimals.</p>
<pre><code>rounded_str = sprintf('%.2f', f)</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

