<?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>Bin-Blog &#187; Ruby</title>
	<atom:link href="http://www.bin-co.com/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bin-co.com/blog</link>
	<description>Learn about the latest in Web Development - as soon as I do.</description>
	<lastBuildDate>Tue, 13 Oct 2009 18:55:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Error Connecting to MySQL using Ruby on Rails in Linux &#8211; and its Solution</title>
		<link>http://www.bin-co.com/blog/2009/04/error-connection-mysql-ruby-rails-linux-solution/</link>
		<comments>http://www.bin-co.com/blog/2009/04/error-connection-mysql-ruby-rails-linux-solution/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 18:22:52 +0000</pubDate>
		<dc:creator>Binny V A</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[troubleshoot]]></category>

		<guid isPermaLink="false">http://www.bin-co.com/blog/?p=249</guid>
		<description><![CDATA[I am currently working on a new Rails project. Its been a while since I worked with rails - so I updated Rails using the command '<code>sudo gem update --system</code>'. I started the inbuilt server and tried to open a page - and to my surprise, I found that <strong class="highlight">Rails is having trouble connecting to the MySQL</strong> server. It was apparent that the <strong class="highlight">mysql gem was missing</strong>. So I tried to install it using the command '<code>sudo gem install mysql</code>'. That failed too - this is the error I got...]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.bin-co.com/blog/wp-content/uploads/2008/05/ruby_logo.gif" alt="Ruby Logo" title="Ruby Logo" width="105" height="109" class="alignnone size-full wp-image-105 intro" align="right" /></p>
<p class="intro">I am currently working on a new Rails project. Its been a while since I worked with rails &#8211; so I updated Rails using the command &#8216;<code>sudo gem update --system</code>&#8216;. I started the inbuilt server and tried to open a page &#8211; and to my surprise, I found that <strong class="highlight">Rails is having trouble connecting to the MySQL</strong> server. It was apparent that the <strong class="highlight">mysql gem was missing</strong>. So I tried to install it using the command &#8216;<code>sudo gem install mysql</code>&#8216;. That failed too &#8211; this is the error I got&#8230;</p>
<pre>Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby
        --with-mysql-config
        --without-mysql-config
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out</pre>
<p>After a couple of hours on Google, I managed to fix the issue &#8211; and I am posting the solution here for others who might run into the same problem.</p>
<h2>Install the necessary Libraries</h2>
<p>First, you must <strong class="highlight">install a couple of mysql libraries</strong>(I am assuming that you have MySQL server already installed). If you are on <strong class="highlight">Red Hat/Fedora/CentOS</strong> etc., run the command&#8230;</p>
<pre><code class="cli">sudo yum install ruby-mysql mysql-devel</code></pre>
<p>If you are on <strong class="highlight">Debian/Ubuntu</strong>/etc., run this command&#8230;</p>
<pre><code class="cli">sudo apt-get install libmysqlclient15-dev libmysqlclient15off libmysql-ruby</code></pre>
<h2>Install the gem</h2>
<p>Once the installation is done, enter this <strong class="highlight">command to install mysql gem</strong>&#8230;</p>
<pre><code class="cli">sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config</code></pre>
<p>If your mysql_config is not in the standard location, update the command with the right path.</p>
<p>The mysql gem should be installed now. These commands worked for me &#8211; your milage may vary. If you still have trouble, try one of the following links&#8230;</p>
<h2>More Resources</h2>
<ul>
<li><a href="http://rails.aizatto.com/2007/05/19/connecting-to-mysql-using-ruby/">Connecting To MySQL using Ruby</a></li>
<li><a href="http://wiki.rubyonrails.org/database-support/mysql">MySQL =&gt; Rails Wiki</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bin-co.com/blog/2009/04/error-connection-mysql-ruby-rails-linux-solution/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hello Script for Ruby</title>
		<link>http://www.bin-co.com/blog/2008/05/hello-script-for-ruby/</link>
		<comments>http://www.bin-co.com/blog/2008/05/hello-script-for-ruby/#comments</comments>
		<pubDate>Thu, 01 May 2008 18:05:39 +0000</pubDate>
		<dc:creator>Binny V A</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hello]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.bin-co.com/blog/?p=104</guid>
		<description><![CDATA[Hello Script is a file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet when working with that language. This Hello Script for Ruby is the sixth post in this series. I have some experience with Ruby. I will not call myself an expert [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.bin-co.com/blog/wp-content/uploads/2008/05/ruby_logo.gif'><img src="http://www.bin-co.com/blog/wp-content/uploads/2008/05/ruby_logo.gif" alt="" title="Ruby Logo" width="105" height="109" align="right" class="intro size-full wp-image-105" /></a></p>
<p class="intro"><a href="http://www.bin-co.com/blog/2008/03/learning-a-new-programming-language-the-hello-world-method/">Hello Script</a> is a <strong class="highlight">file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet</strong> when working with that language. This Hello Script for Ruby is the sixth post in this series.</p>
<p>I have some experience with Ruby. I will not call myself an expert &#8211; but I am comfortable with Ruby. <a href="http://binnyva.blogspot.com/2005/09/learning-ruby.html">I like ruby.</a> I even have written a few <a href="http://www.bin-co.com/ruby/tk/locater/">applications</a> in it.</p>
<h2>Hello Code</h2>
<pre><code class="ruby">
#!/usr/bin/ruby

print "Hello World\n"

name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + year.to_s + "\n"

if (year &gt; 2008) then
	print "Welcome to the future - yes, we have flying cars!"
elsif (year &lt; 2008) then
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
else
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
end
print "\n\n"

# For loop like structure
0.upto(3) { |i|
	print i.to_s + ") Hi there!\n"
}
print "\n"

# Numerical array
rules = ['Do no harm', 'Obey', 'Continue Living']
i = 0
while i&lt;rules.length do
	print "Rule " + (i+1).to_s + ": " + rules[i] + "\n"
	i = i+1
end
print "\n"

# Associated arrays
associated = {
	'hello'	=&gt;	'world',
	'foo'	=&gt;	'bar',
	'lorem'	=&gt;	'ipsum'
}
associated.each { |key,value|
	print key + " : " + value + "\n"
}
print "\n"

# Using Join and Split
csv_values = "hello,world,how,are,you\n".split(",")
print csv_values.join(":")

# Function, argument, return, call
def hello(name)
	return "Hello " + name
end
hello_string = hello("Binny")
print "Function call returned '" + hello_string + "'\n\n"

# One for the OOP fanboys - Class, members, object and stuff.
class Movie
	public
	@name = ''
	@rating = 0

	def initialize(name)
		@name = name
		self.rateMovie()
	end

	def rateMovie()
		@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!
	end

	def printMovieDetails()
		print "Movie : " + @name + "\n"
		print "Rating : " + '*' * @rating + "(" + @rating.to_s + ")\n\n"
	end
end
# Create the object
ncfom = Movie.new("New Country for Old Men") #It's a sequel!
ncfom.printMovieDetails()

# File IO
# File reading, easy method...
file_in = File.new('Hello.rb', 'r')
contents = file_in.read
file_in.close
print "Hello has " + contents.length.to_s + " chars\n"

# Writing to a file
file_out = File.new('/tmp/hello.txt', 'w')
file_out.print "Hello World from Ruby."
file_out.close

# Command Executing
print `ls` #Execute the command 'ls' and print its output
print "\n"

# Regular Expressions
string = "Hello World"
print "Yup - its evil\n" if(/^Hell/.match(string))
print string.gsub(/l([^l])/, '\1') #Remove an 'l' from both words. Should print 'Helo Word' - The second arg must be in single quotes

print "\n"

# Some special/only-in-ruby stuff...
# Using a library
require "fileutils"

#Using yield/code blocks...
def doXTimes(i)
	0.upto(i) {|count|
		yield count+1
	}
end
doXTimes(5) {|count|
	print count.to_s + "th Time\n"
}

</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bin-co.com/blog/2008/05/hello-script-for-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating PDF in Ruby on Rails &#8211; PDF::Writer</title>
		<link>http://www.bin-co.com/blog/2007/11/creating-pdf-in-ruby-on-rails-pdfwriter/</link>
		<comments>http://www.bin-co.com/blog/2007/11/creating-pdf-in-ruby-on-rails-pdfwriter/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 17:45:18 +0000</pubDate>
		<dc:creator>Binny V A</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Writer]]></category>

		<guid isPermaLink="false">http://www.bin-co.com/blog/2007/11/creating-pdf-in-ruby-on-rails-pdfwriter/</guid>
		<description><![CDATA[PDF::Writer is my choice for creating PDF files in Ruby on Rails. Its simple, easy to use, and has all the features I am looking for.]]></description>
			<content:encoded><![CDATA[<p class="intro">PDF::Writer is my choice for creating PDF files in Ruby on Rails. Its simple, easy to use, and has all the features I am looking for.</p>
<p>Install PDF::Writer in Linux systems using this command&#8230;</p>
<p><code>gem install pdf-writer -y</code></p>
<h3>Includes</h3>
<p>We will require rubygems and pdf/writer.</p>
<pre><code class="ruby">require "rubygems"
require "pdf/writer"
</code></pre>
<p>The next line creates a new instance of PDF::Writer</p>
<pre><code class="ruby">pdf = PDF::Writer.new</code></pre>
<h3>Create a Heading(big font size)</h3>
<pre><code class="ruby">pdf.select_font "Times-Roman"
pdf.text "Sample PDF Document", :font_size => 32, :justification => :center
</code></pre>
<p>The text function will add a string of text to the document, starting at the current position. It will wrap to keep within the margins &#8211; so you can specify text as big blocks. The text will go to the start of the next line when a return code &#8220;\n&#8221; is found.</p>
<p>The other arguments available for this function are&#8230;</p>
<dl>
<dt>:font_size</dt>
<dd>The font size to be used. If not specified, is either the last font size or the default font size of 12 points. Setting this value changes the current font_size.</dd>
<dt>:left</dt>
<dd>Gap to leave from the left margin</dd>
<dt>:right</dt>
<dd>Gap to leave from the right margin</dd>
<dt>:absolute_left</dt>
<dd>Absolute left position (overrides :left)</dd>
<dt>:absolute_right</dt>
<dd>Absolute right position (overrides :right)</dd>
<dt>:justification</dt>
<dd>This can be :left, :right, :center, :full</dd>
<dt>:leading</dt>
<dd>This defines the total height taken by the line, independent of the font height.</dd>
<dt>:spacing</dt>
<dd>Line spacing &#8211; usually set to one of 1, 1.5, 2 (line spacing as used in word processing)</dd>
</dl>
<h3>Writing Text</h3>
<pre><code class="ruby">pdf.text "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam sodales velit ac ante. Suspendisse felis mi, convallis at, semper id, malesuada eu, mauris. Integer orci. Sed consectetuer orci. In hac habitasse platea dictumst. Duis nec pede. Ut lacinia eros ut magna. Maecenas lectus dui, lacinia vel, porttitor a, fringilla nec, turpis. Nulla odio nisi, mattis ac, porttitor at, malesuada ac, nibh. Nam suscipit mi ut justo. Phasellus aliquam lorem non velit ornare bibendum. Nullam mollis. Ut elementum rutrum justo. Pellentesque ac sapien. In facilisis lorem a enim. Curabitur vitae felis. In eget tellus nec ligula egestas semper. Nulla facilisis urna nec magna. Pellentesque fringilla pulvinar risus. Aliquam rutrum, nisi ut lobortis consequat, nisl felis posuere risus, at sollicitudin nibh dui et felis.

Fusce tristique dapibus neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut felis mi, dictum ut, vehicula non, fermentum quis, elit. Quisque ultricies purus quis enim. Integer turpis elit, porttitor quis, volutpat consequat, interdum vitae, quam. Donec tempus, dolor eget bibendum euismod, metus dolor imperdiet purus, vitae nonummy est mi non orci. Aenean eu massa. Fusce euismod. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nullam quis massa id sem commodo eleifend. Cras eu velit rutrum leo egestas adipiscing. Nulla volutpat, lectus sit amet sagittis gravida, erat tortor condimentum sem, vitae sollicitudin lectus diam eget felis. Aliquam augue. Vestibulum viverra est. Fusce tellus ligula, euismod sed, placerat vel, cursus dictum, erat.", :font_size => 12
</code></pre>
<p>I used the text() &#8211; but this time with 12 as the font_size. The long text will wrap around and the \n will be converted to line breaks.</p>
<h3>Inserting Images</h3>
<p>PDF::Writer provides an easy way to insert images&#8230;</p>
<pre><code class="ruby">pdf.image "default.png"</code></pre>
<p>That&#8217;s it &#8211; it will insert an image called &#8216;default.png&#8217;. The other option are&#8230;</p>
<dl>
<dt>:pad</dt>
<dd>The number of PDF userspace units that will be on all sides of the image. The default is 5 units.</dd>
<dt>:width</dt>
<dd>The desired width of the image. The image will be resized to this width with the aspect ratio kept. If unspecified, the imageâ€™s natural width will be used.</dd>
<dt>:resize</dt>
<dd>How to resize the image, either :width (resizes the image to be as wide as the margins) or :full (resizes the image to be as large as possible). May be a numeric value, used as a multiplier for the image size (e.g., 0.5 will shrink the image to half-sized). If this and :width are unspecified, the imageâ€™s natural size will be used. Mutually exclusive with the <tt>:width</tt> option.</dd>
<dt>:justification</dt>
<dd>The placement of the image. May be :center, :right, or :left. Defaults to :left.</dd>
<dt>:border</dt>
<dd>The border options. No default border. If specified, must be either true, which uses the default border, or a Hash.</dd>
</dl>
<h3>Tables</h3>
<p>Inserting tables are slightly more complicated than the last two items. If you are using SimpleTable, you have to add<br />
<code>require "pdf/simpletable"</code><br />
along with the other includes.</p>
<pre><code class="ruby">table = PDF::SimpleTable.new
table.title = "Sample Tables"
table.column_order.push(*%w(first second))

table.columns["first"] = PDF::SimpleTable::Column.new("first")
table.columns["first"].heading = "First"

table.columns["second"] = PDF::SimpleTable::Column.new("second")
table.columns["second"].heading = "Second"

table.show_lines    = :all
table.show_headings = true
table.orientation   = :center
table.position      = :center

data = [
	{"first"=> "1", "second"=> "2"}, # First row
	{"first"=> "One", "second"=> "Two"}, # Second row
	{"first"=> "Mono", "second"=> "Di"}, # Third row
]

table.data.replace data
table.render_on(pdf)</code></pre>
<p>The above code will create a table with two column and three rows of data. The code should be easy to decipher &#8211; so I am not going into any more explanation.</p>
<h3>Saving the PDF</h3>
<p>Finally, saving the created PDF&#8230;</p>
<pre><code class="ruby">pdf.save_as("report.pdf") </code></pre>
<h2>Links</h2>
<ul>
<li><a href="http://www.artima.com/rubycs/articles/pdf_writer.html">Tutorial</a></li>
<li><a href="http://ruby-pdf.rubyforge.org/pdf-writer/">Website</a></li>
<li><a href="http://ruby-pdf.rubyforge.org/pdf-writer/manual/manual.pdf">Manual (PDF)</a></li>
<li><a href="http://ruby-pdf.rubyforge.org/pdf-writer/doc/index.html">API Documentation</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bin-co.com/blog/2007/11/creating-pdf-in-ruby-on-rails-pdfwriter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

