<?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>RubyLearning Blog &#187; Julio Javier Cicchelli</title>
	<atom:link href="http://rubylearning.com/blog/author/juliojaviercicchelli/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubylearning.com/blog</link>
	<description>Helping Ruby Programmers become Awesome</description>
	<lastBuildDate>Thu, 23 May 2013 22:05:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>So&#8230; you&#8217;re new to Ruby!</title>
		<link>http://rubylearning.com/blog/2010/09/24/so-youre-new-to-ruby/</link>
		<comments>http://rubylearning.com/blog/2010/09/24/so-youre-new-to-ruby/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 23:00:59 +0000</pubDate>
		<dc:creator>Julio Javier Cicchelli</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Masters]]></category>
		<category><![CDATA[Javier Cicchelli]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=4689</guid>
		<description><![CDATA[Send to Kindle So&#8230; you&#8217;re new to Ruby! This guest post is contributed by Javier Cicchelli, a Software Engineer at Rock &#38; Code, the Software and Marketing communications shop that rocks! Currently, they are doing their dirty deeds and conducting their high voltage operations in the (in)famous Red Light District in Amsterdam, The Netherlands. Some [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
				<content:encoded><![CDATA[<div class='kindleWidget kindleLight' ><img src="http://rubylearning.com/blog/wp-content/plugins/send-to-kindle/media/black-25.png" /><span>Send to Kindle</span></div><p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2010%252F09%252F24%252Fso-youre-new-to-ruby%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F9G1rkQ%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22So...%20you%27re%20new%20to%20Ruby%21%22%20%7D);"></div>
<div>
<h3>So&#8230; you&#8217;re new to Ruby!</h3>
<p class="update">This guest post is contributed by <strong><a href="http://twitter.com/monsieur_rock">Javier Cicchelli</a></strong>, a Software Engineer at <a href="http://rock-n-code.com">Rock &amp; Code</a>, the Software and Marketing communications shop that rocks! Currently, they are doing their dirty deeds and conducting their high voltage operations in the (in)famous Red Light District in Amsterdam, The Netherlands. Some four years ago, he started developing all the symptoms of a peculiar case of programmer&#8217;s schizophrenia. It all happened when he got a copy of the first edition of <a href="http://pragprog.com/titles/rails4/agile-web-development-with-rails">&#8220;Agile Web Development with Ruby on Rails&#8221;</a> by <a href="http://twitter.com/pragdave">Dave Thomas</a> and <a href="http://twitter.com/dhh">David Heinemeier Hansson</a>. Because of this book, he began spending a lot of his nights hacking with this amazing new technology. Back then, he was using C# and VB.NET at work and at some point, he simply started feeling somehow uncomfortable with it. Indeed, he was quite disappointed with the way the .NET framework and its languages was evolving. So in order to vent out his frustration, he unconsciously turned to the only relief he had: <a href="http://ruby-lang.org">Ruby</a>. Indeed, the more vexed he got with .NET, the more he was getting into Ruby. At that point, he discovered the vibrant Ruby community in the Ranstad region. An year ago, he got enough courage to quit his daily job and found his company, <a href="http://rock-n-code.com">Rock &amp; Code</a>. He did so, determined to use all those new technologies available and simply rock development to the top.</p>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright" src="http://www.rubylearning.com/images/jjcicchelli.jpg" alt="Julio Javier Cicchelli" width="125" height="125" /> <span class="drop_cap">N</span>ow that you know a bit more about me, let’s get down to business and dive in. First of all, welcome to Ruby! There might be multiple reasons why you are reading this entry; you might want to get your hands dirty with Ruby, you could be just curious as to what this whole Ruby thing is all about, or you might be simply bored with the rest of the technologies out there and you are in a search for a new technological medicine. The good news is that if you get hooked on Ruby and you decide to explore it further, there are plenty of useful docs, tons of blog entries and numerous nice fellas that can help you along the way. I have been there, I have done that. In addition, every single day, I learn something new by reading Ruby-related books and blog posts and by spending some time talking with fellow rubyists on mailing lists, IRC and Twitter. In order to add certain value to my article, I will attempt to cover the basic stepping stones for Ruby beginners.</p>
<h3>What did I get myself into?</h3>
<p>Before going any further, you should be warned what you are about to mess with.</p>
<p>In a nutshell, Ruby is an extremely high-level programming language created in Japan by <a href="http://twitter.com/yukihiro_matz">Yukihiro &#8220;Matz&#8221; Matsumoto</a> almost 20 years ago. It is heavily influenced by some of the most popular Scripting, Object Oriented, and Functional languages. It is built on C and its utter goal is to make programming fun again by obfuscating its vast complexity into natural syntactic sugar for the masses. Devised mainly as a Object-Oriented language, Ruby considers (almost) everything as objects that can have attributes and methods. Even though it might seem restrictive as it supports simple inheritance only, Ruby considers that modules containing methods could be plugged-in into classes or objects and, therefore, it could use these methods as if they were its own. This is a concept known as &#8220;Mixin&#8221;. Furthermore, this language provides you with a great deal of flexibility, which means you are able to alter any class, module or object on-the-fly by adding, modifying or removing functionality dynamically. In addition, its support for blocks gives greater flexibility and expressiveness to your methods in a functional fashion. Moreover, it does not require you to specify the types you are going to use in your code in advance. Nor does it require you to handle the memory allocation yourself. Ruby is not the kind of language that limits you and allows you to write your code in just one way. In fact, you can write your code in either an imperative, a procedural, an object-oriented or even a functional fashion.</p>
<hr />New to Ruby? Join our online Core Ruby course. Read details <a href="http://rubylearning.com/blog/2010/09/06/new-course-ruby-programming-101/">here</a>.</p>
<hr />Even though Ruby is mainly used for creating web applications due to the widespread popularity of <a href="http://www.rubyonrails.org">Ruby on Rails</a>, it is actually a general-purpose programming language and it can be used in almost any kind of industry. Little by little, Ruby has been gaining a lot of momentum and it has been slowly but surely incorporated into the enterprise world. The fact that it makes developers happy is reflected in the way software has been produced. Indeed, if every member of a development team is able to quickly write simple code that is close to the natural language and it describes the domain of the problem, this will certainly have positive impact on the way this code is read, understood and maintained. Any beginner with basic notions of Object-Oriented programming should be able to catch up quickly, regardless of the kind of project.</p>
<p>Logically, there is always a price to pay and this is no exception: interpreted languages are usually much slower than their compiled counterparts and making developers happy has a direct impact on the overall performance of the interpreter. In addition, the way the interpreter uses multi-core processors, handles threading, and implements the memory garbage collector strategy usually raise concerns among some developers and industries which requires extremely high processing capabilities. Another problem is the lack of an established GUI that would allow developers to create cross-platform applications for either desktop, server and mobile environments. As you can imagine, these disadvantages are usual referred to when somebody starts the typical war of words “Ruby vs. (fill in the blanks with your language of choice)” on Hacker News or any similar site. Evidently, this somehow undermines the importance of Ruby as an important player in the field of Programming Languages of the decade.</p>
<p>Because of the Open Source nature of Ruby, a handful of great hackers and a few courageous people have forked the MRI (the de-facto Matz Ruby Interpreter) in order to propose their own solutions to the aforementioned cons. In the meantime, they have added some real value for developers of a certain niche platform. For example, <a href="http://www.rubyenterpriseedition.com/">REE</a> optimizes the garbage collector of the MRI for web applications and <a href="http://rubini.us">Rubinius</a> is a reimplementation of the MRI as a virtual machine a la Smalltalk, which aims at solving most of the known disadvantages of Ruby. Still other projects such as <a href="http://jruby.org">JRuby</a>, <a href="http://ironruby.net">IronRuby</a> and <a href="http://www.macruby.org">MacRuby</a> emphasize on a really tight integration with the Java, .NET and the OSX platforms.</p>
<p>Still, Ruby has a really friendly and thriving community behind its back that is systematically pushing its development and evolution forward. If you ask me, Ruby certainly has a couple of aces up its sleeve.</p>
<h3>How to install Ruby on my machine?</h3>
<p>Although some Operating Systems have some version of the MRI interpreter already installed, I would highly recommend that you use <a href="http://rvm.beginrescueend.com">RVM</a> to install, administer, and use multiple Ruby interpreters and set of gems very easily. This utility, written by <a href="http://twitter.com/wayneeseguin">Wayne E. Seguin</a>, aims at addressing the most common, yet painful issues of developing, testing ,and deploying on Ruby, especially if you are looking forward to supporting different versions of Ruby. It does not really matter if you are working on your development or production environment. The RVM resolves complex issues regarding different versions seamlessly and it provides you with a simple and concise API that does not interfere with your tasks at hand. Definitely, this is a tool worth studying carefully because it will surely save you lots of work and time. It is also good for your mental health.</p>
<p>Unfortunately, RVM is not supported out-of-the-box on Windows environments and as far as I know, I do not think there is any alternative to this environment so you should stick to the <a href="http://rubyinstaller.org">Ruby Installer</a> executables for the moment, which will install the MRI on your machine. Unfortunately, it will not give you the great deal of functionality described above. If any of you knows of a similar solution for Windows environments, please write a comment below. Lots of people will surely be interested in this.</p>
<p>Even though it is not specifically related to Ruby, <a href="http://git-scm.com">Git</a> is definitely one of the most powerful tools at your disposal today. It is a quick, efficient, and easy-to-use distributed version control system that allows you to manage the changes of your code over time. Basically, you have a local copy of your project with the history of your changes and you can import those changes into another repository as branches that can be merged with the local content. Git was created for non-linear development, so it allows you to create convenient branches, produce quick merges, and visualize the changes over time. Unlike RVM, Git is very well supported by every major environment out there so rejoice!</p>
<h3>Ok&#8230; now what?</h3>
<p>Probably it is the most common question you may ask to yourself right after installing on your machine whatever versions of the Ruby interpreter you have chosen.</p>
<p>Whether you&#8217;re an adventurous developer or not, I highly recommend that you read the <a href="http://mislav.uniqpath.com/poignant-guide/book/">“Why&#8217;s (poignant) Guide to Ruby”</a> by the now missing in action <a href="http://www.smashingmagazine.com/2010/05/15/why-a-tale-of-a-post-modern-genius/">Why the Lucky Stiff</a>. This is the most unorthodox, yet extremely didactic programming book I have ever had the pleasure to read so far. No matter how experienced you are in the art of coding, read it and enjoy! This book is the very embodiment of the spirit of the Ruby language and its community. Simply awesome. Of course, there are hundreds of books about Ruby out there, mostly for beginners, but really, take my piece of advice and try this one first. You will not regret it!</p>
<p>One of the best things you can do while reading either a book or a post blog related to Ruby, is to test the code on the <a href="http://www.rubyinside.com/irb-lets-bone-up-on-the-interactive-ruby-shell-1771.html">IRB</a>, the interactive Ruby shell. This tool (or its equivalent on the different versions of the interpreter) executes every piece of code you insert in it, one line at the time. Believe me, trying every code example you may find in your way would really give you a better understanding of how Ruby works. On top of it, it is real fun!</p>
<h3>What can I do with Ruby?</h3>
<p>Although the main use of Ruby is focused on web development due to the popularity of the Ruby on Rails framework, you are able to easily create almost all the types of software for work and have tons of fun in the mean time. From tiny libraries and <a href="http://www.infoq.com/news/2007/06/dsl-or-not">DSLs</a> to complex frameworks and applications, Ruby is a general-purpose programming language that does not get in the way between your requirements and the actual implementations.</p>
<p>In any case, the best way to understand what you can do with this language is by adopting an empirical approach. Experiencing the pros and cons of the Ruby language yourself would allow you to become an expert in it.</p>
<h3>Which way to go from here?</h3>
<p>If there is something that Ruby normally offers to its developers, this is a whole range of choices, lots of choices&#8230; sometimes too many choices. Apart from the choices you may have to make in order to settle on with a text editor, some databases engines, a particular web server, etc., Ruby does not have just one version of the interpreter. Nor does it provide only one way of doing things. As you may have already guessed, neither do the developers out there.</p>
<p>There are many techniques, resources, and approaches to write Ruby code and since most of the Ruby code is mostly open source and it is available on <a href="http://github.com">Github</a> (this is where Git comes along!), you can take a look at this treasure trove and learn a lot from it. Just remember: do not be overwhelmed by the huge number of choices that awaits you.</p>
<h3>Should I write tests all the time?</h3>
<p>Ruby has certainly taken testing back from obscurity and it has re-introduced it to the forefront of development. This is the time I confessed something: before learning Ruby, testing was the task that I was always trying to avoid because writing tests on .NET using NUnit is not the most exciting task in the world. Nonetheless, testing in Ruby using any library, regardless of whether you are using <a href="http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/">Test Unit</a>, <a href="http:// rspec.info">Rspec</a>, <a href="http://cukes.info">Cucumber</a> or else is not an easy task either. Usually, it takes a lot of time and quite a substantial experience to learn to write really good tests. It is certainly an overhead in your development. So, if you plan to play with the language, develop proofs of concept, and try out code, I really do not recommend that you dive into testing right away. Take your time to learn the language first. When you start writing your own libraries or collaborate on somebody else’s project, then you should take a look at the existing testing libraries, make an educated choice, and be really patient.</p>
<p>In any case, you should realize that Testing is really important. You should always ensure that your code verifies and validates both inputs and outputs, fails when it has to fail, and works when it has to work. Indirectly, testing is a form of documenting your code, because it basically shows how your code should work in a somehow realistic environment. In my opinion, testing is a long-term investment in your code for you and the people involved in your project. If you are really interested in how testing can help you structure the way to develop Software, then you should take a look at the <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> and <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a> techniques.</p>
<p>I think that my explanations have given you enough food for thought. Now you are prepared to start your quest in the land of the Rubies. Fear not! Be confident and ready to experiment. You will not regret it. I promise you will have lots of fun sticking around! My final piece of advice to you is that you should really know your tools very well, no matter what kind of a text editor, database, framework, library, DSL or anything else you choose to work with.</p>
<p><em>Thanks a lot for the attention, boys and girls! I hope you enjoyed reading this post as much as I enjoyed writing it. I wish you good luck in your Ruby adventure. Most probably, our paths will cross along the way. If you have any further questions, please do not hesitate to post them as comments here. In the meantime, stay tuned for the other great blog entries of the Ruby Gurus series and enjoy learning Ruby on RubyLearning!</em></p>
<p class="alert"><strong><em>Post supported by Blue Box Group</em>:</strong> <a href="http://www.blueboxgrp.com/?utm_source=rubylearning&amp;utm_medium=blog&amp;utm_campaign=rubylearning">Blue Box Group</a> is in the business of providing affordable <a href="http://www.blueboxgrp.com/?utm_source=rubylearning&amp;utm_medium=blog&amp;utm_campaign=rubylearning">Rails hosting</a> solutions! They approach web hosting, virtual servers and dedicated servers differently, treating each client as a partner and working towards the common goal of success for their business. They have the experience, talent and equipment to make your site a success!</p>
<p><b><em>Do read</em> these awesome Guest Posts:</b></p>
<ul>
<li><a href="http://rubylearning.com/blog/2010/09/23/incorporating-web-apis-to-spark-computer-programming-exercises/">Incorporating Web APIs to spark computer programming exercises</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/22/14-ways-to-have-fun-coding-ruby/">14 Ways To Have Fun Coding Ruby</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/21/writing-modular-web-applications-with-rack/">Writing modular web applications with Rack</a></li>
<li><a href="http://rubylearning.com/blog/2010/09/20/how-to-learn-ruby-or-any-programming-language/">How to Learn Ruby (or any programming language)</a></li>
</ul>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag">Ruby</a>, <a href="http://technorati.com/tag/Programming" rel="tag"> Programming</a>, <a href="http://technorati.com/tag/Javier+Cicchelli" rel="tag"> Javier Cicchelli</a></p>
Posted by <b>Julio Javier Cicchelli</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2010/09/24/so-youre-new-to-ruby/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
		<item>
		<title>Cookie-based Sessions in Sinatra</title>
		<link>http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/</link>
		<comments>http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 01:06:34 +0000</pubDate>
		<dc:creator>Julio Javier Cicchelli</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Learn Sinatra]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Julio Javier Cicchelli]]></category>

		<guid isPermaLink="false">http://rubylearning.com/blog/?p=3047</guid>
		<description><![CDATA[Send to Kindle Cookie-based Sessions in Sinatra This is a guest post from Julio Javier Cicchelli. Hi there everybody! My name is Javier Cicchelli and I am the Software Engineer of the technological pride and joy of the Red Light District in Amsterdam, the Netherlands: Rock &#38; Code. I have been vested with the distinct [...]<p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>
]]></description>
				<content:encoded><![CDATA[<div class='kindleWidget kindleLight' ><img src="http://rubylearning.com/blog/wp-content/plugins/send-to-kindle/media/black-25.png" /><span>Send to Kindle</span></div><p></p>
<div class="topsy_widget_data topsy_theme_brick-red" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frubylearning.com%252Fblog%252F2009%252F09%252F30%252Fcookie-based-sessions-in-sinatra%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Cookie-based%20Sessions%20in%20Sinatra%22%20%7D);"></div>
<div>
<h3>Cookie-based Sessions in Sinatra</h3>
<p class="block"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright" title="Julio Javier Cicchelli" src="http://www.rubylearning.com/images/jjcicchelli.jpg" alt="Julio Javier Cicchelli" />This is a guest post from <b><a href="http://rubylearning.com/blog/2009/07/20/julio-javier-cicchelli-how-do-i-learn-and-master-sinatra/">Julio Javier Cicchelli</a></b>.</p>
<p>Hi there everybody! My name is <a href="http://twitter.com/monsieur_rock" >Javier Cicchelli</a> and I am the Software Engineer of the technological pride and joy of the Red Light District in Amsterdam, the Netherlands: <a href="http://rock-n-code.com" >Rock &amp; Code</a>. I have been vested with the distinct authority to shed some light on the concept of <em>Cookie</em>. The purpose of this article is to teach you how to concoct the explosive digital mixture that would enable you to use cookies for sessions within your <a href="http://www.sinatrarb.com/" >Sinatra</a> applications. Piece of cake, right? Before I move to the nitty-gritty of cookies in Sinatra, I want to cover the mandatory theoretical basics. So, I invite you to sit back and enjoy this slow and easy software ride. By the end of that piece, you will be asking for cookies at the nearest bakery!</p>
<h3>What are Cookies?</h3>
<p>According to the Computer Science definition, a <em>cookie</em>, which is also known as an <em>HTTP cookie</em>, a <em>tracking cookie</em>, or a <em>browser cookie</em>, is a piece of text, no bigger than 4 kilobytes, which is stored on the user&rsquo;s computer by a web server via a web browser. It is a key-value pair structure, which is designed to retain specific information such as user preferences, user authentication, shopping carts, demographics, sessions, or any other data used by a website. This mechanism, which was developed by <a href="http://netscape.aol.com/" >Netscape</a> in the distant 1994, provides a way to receive information from a web server and to send it back from the web browser absolutely unchanged. This system complements the stateless nature of the HTTP protocol as it provides enough memory to store pieces of information during HTTP transactions.</p>
<p>When you try to access a web site, your web browser connects to a web server and it sends a request for the respective page. Then the web server replies by sending the requested content and it simultaneously stores a new cookie on your computer. Every time the web browser requests web pages from the web server, it always sends the respective cookies back to the web server. The process takes place as described, if the web browser supports cookies and the user allows their usage. Only the web server can modify one or more of the cookie values. Then it sends them to the web browser upon replying to a specific request.</p>
<p>According to the <a href="http://rfc.dotsrc.org/rfc/rfc2965.html" >RFC2965 specification</a>, cookies are case insensitive. A set of defined properties is inherent to the cookie structure. Those properties include an expiration date, a path and a domain. The first attribute requires a date defined in <em>Wdy, DD-Mon-YYYY HH:MM:SS GMT</em> format. The rest of the cookie characteristics require a path and/or a domain defined as a string. Let&rsquo;s take a look at this example:</p>
<pre>Cookie: key0=value0; ...; keyX=valueX; expires=Wed, 23-Sep-2009 23:59:59 GMT; path=/; domain=.yoursite.com
</pre>
<p>When the expiration date is defined, your cookie will be &quot;persistent&quot; as it will reoccur in different sessions until the set expiration date has been reached. If the expiration date has not been defined in the cookie, it will occur until the end of your current session or when you close your web browser. If the path and/or the domain attributes have been defined in your cookie, then the web server limits the scope of the cookie to that specific domain, sub-domain or path.</p>
<h3>Cookies and Sinatra</h3>
<p>Having clarified the basics, we can proceed to the next level. Prepare to learn how to implement cookie-based sessions in the Sinatra web framework. You do not have to install any extra Ruby gem.</p>
<p>Get ready to rumble! First and foremost, you have to decide on the session strategy your Sinatra application will use in order to send to the web browser only those relevant pieces of information that your application requires. As I have already explained, this information depends on the kind of application you want to develop and on the functionalities you want to offer to your users. Then you have to enable the cookie based session function in Sinatra. Finally, after enabling the cookie-based support, you have to implement the designed session strategy in your application. Regardless of how you want to develop your application, you must always test your code in order to verify that it works as expected and validate that it satisfies all the given requirements. Throughout the remainder of this article, I will look at the existing methods that provides Sinatra for using sessions based on cookies.</p>
<h3>A Simple Example</h3>
<p>Let&rsquo;s take it slow and easy. I will commence with a very simple example. I will write a cookie version of the famous &quot;Hello World&quot; example. Here I will use the cookie functions included in the Sinatra framework in order to demonstrate how to use them. In the following example, I will explain how to enable the session support in your Sinatra application and I will show you how to create a key-value pair inside the session. So, let&rsquo;s rock &amp; code!</p>
<pre>require 'rubygems'
require 'sinatra'

enable :sessions

get '/' do
  session["value"] ||= "Hello world!"
  "The cookie you've created contains the value: #{session["value"]}"
end
</pre>
<h3>A Slightly Bigger Example</h3>
<p>Now that you have a basic idea of how cookies work in Sinatra, you are ready to move to the next level. I will write a more complex example, which will consist of a tiny web application, which will ask the user to identify. In this example, I will use the session support provided by Sinatra in order to implement non-persistent cookies. Note that I am using the technique I resorted to in the previous example. I simply tuned it to the logic of this application.</p>
<pre>require 'rubygems'
require 'sinatra'
require 'haml'

enable :sessions

get '/' do
  session["user"] ||= nil
  haml :index
end

get '/introduction' do
  haml :introduction
end

post '/introduction' do
  session["user"] = params[:name]
  redirect '/'
end

get '/bye' do
  session["user"] = nil
  haml :bye
end
</pre>
<h3>A Final Example</h3>
<p>The last example will demonstrate how to directly manage cookies through the <em>request</em> and <em>response</em> singletons provided by Sinatra. You will see in the following example that the previously described process involving the use cookies is clearly implemented. This technique is recommended when your application requires to use persistent and/or scoped cookies. In this example, the application uses two persistent cookies, which expire at the same time, in order to store and manage different configuration data. Unfortunately, due to a bug on the cookie management that I still have to investigate, I cannot test how the application responds to the scope of the different cookies it has created.</p>
<pre>require 'rubygems'
require 'sinatra'
require 'haml'

get '/' do
  @@expiration_date = Time.now + (60 * 2) \
  unless request.cookies.key?('some_options') &#038;&#038; request.cookies.key?('other_options')
  haml :index
end

get '/some_options' do
  @some_cookie = request.cookies["some_options"]
  haml :some_options
end

post '/some_options' do  
  response.set_cookie('some_options', :value => cookie_values(params), :expires => @@expiration_date)
  redirect '/'
end

get '/other_options' do
  @other_cookie = request.cookies["other_options"]
  haml &#58;other_options
end

post '/other_options' do
  response.set_cookie('other_options', :value => cookie_values(params),:expires => @@expiration_date)
  redirect '/'
end

helpers do
  def cookie_values(parameters)
    values = {}
    parameters.each do |key, value|
      case key
      when 'options'
        values[value] = true
      else
        values[key] = true
      end
    end
    values
  end
end
</pre>
<p>I would like to stress that Sinatra does allow developers to use directly <a href="http://rack.rubyforge.org/" >Rack</a> to manage cookie through the <em>Rack::Session::Cookie</em> middleware. Yet, I desisted from discussing this method because of its complexity. Furthermore, I have not elaborated on the existence of other alternatives to this storage mechanism. After all, I have to leave some room for upcoming articles I can present you with. Do not allow my deliberate slips to limit your curiosity! Be inquisitive! You can see the source code on <a href="http://gist.github.com/205962" >Github</a>, of all the relevant examples. So go ahead and rock &amp; code!</p>
</div>
<p>Technorati Tags: <a href="http://technorati.com/tag/Ruby" rel="tag">Ruby</a>, <a href="http://technorati.com/tag/Sinatra" rel="tag">Sinatra</a>, <a href="http://technorati.com/tag/Cookies" rel="tag">Cookies</a>, <a href="http://technorati.com/tag/Julio+Javier+Cicchelli" rel="tag">Julio Javier Cicchelli</a></p>
Posted by <b>Julio Javier Cicchelli</b><p><a href="http://www.launchbit.com/az/113-209/"><img width="468" height="60" src="http://www.launchbit.com/az-images/113-209/" /></a><br />
<small>(Powered by <a href="http://www.launchbit.com/lb/113-209/">LaunchBit</a>)</small></p>

]]></content:encoded>
			<wfw:commentRss>http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
