citizen428.blog()

Try to learn something about everything

My Way: Ruby, Sinatra, Heroku

RubyLearning just had a nice one week introductory course on Sinatra, a DSL for easily creating web applications. The next batch will start on May 2nd (and I’ll probably be around as a mentor again), you can join here if you are interested.

Anyway, since it’s so easy to write applications with Sinatra, you probably don’t want to waste time deploying them. That’s where Heroku comes into play, a Ruby hosting platform which modeled it’s workflow around git. In my – still somewhat limited – experience working with Heroku is painless and feels very natural, kudos to the team for providing us with such a nice option for hosting Rack based apps! In fact I liked Heroku so much, that I wrote a patch for Aaron Quint‘s sinatra-gen which will initialize git and create your app on Heroku for you. It’s not in the main branch yet, but Aaron said he likes the idea and that he will include it. Up until then you can get my version from here.

Update: Said patch – in slightly modified form – is now part of mainline sinatra-gen, so I recommend getting the “official” version, not mine.

Oh yeah, once you are done creating all this shiny new webapps in Sinatra, don’t forget to take some snapshots of them with snapurl, another nifty little app I recently wrote a small patch for.

Create Script From IRB Session

We all know the feeling: before starting a new script you hack around in IRB to try out some things, but once you’re done you realize that now you either have to type everything again or copy/paste from the IRB session (or the history file if you keep one). So I set out on my search for a solution and found an old ruby-talk post by Gavin Sinclair, which implemented a method called dumphist, which I modified to my needs and put in my ~/.irbrc:

Let’s look at the following IRB session:

1
2
3
4
5
6
7
8
9
10
11
12

1
2
3
4
5
6
7
8
9
10
11
12
<span class='line'><span class="o">&gt;&gt;</span> <span class="nb">puts</span> <span class="s2">&quot;hello&quot;</span>
</span><span class='line'><span class="n">hello</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">nil</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="nb">puts</span> <span class="s2">&quot;world&quot;</span>
</span><span class='line'><span class="n">world</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">nil</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="k">def</span> <span class="nf">foo</span>
</span><span class='line'><span class="o">.</span><span class="n">.</span>   <span class="nb">puts</span> <span class="s1">&#39;bar&#39;</span>
</span><span class='line'><span class="o">.</span><span class="n">.</span>   <span class="k">end</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="kp">nil</span>
</span><span class='line'><span class="o">&gt;&gt;</span> <span class="n">dumphist</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;test.rb&#39;</span><span class="p">)</span>
</span><span class='line'><span class="o">=&gt;</span> <span class="o">[</span><span class="s2">&quot;def foo&quot;</span><span class="p">,</span> <span class="s2">&quot;puts &#39;bar&#39;&quot;</span><span class="p">,</span> <span class="s2">&quot;end&quot;</span><span class="o">]</span>
</span>

The last command created a file named test.rb for us, which contains only the method foo. :-)

Ruby Implementations and Why We Should Care

Not too lang ago our favorite language had only implementation, the one made by Matz (now commonly referred to as MatzRuby or MRI). This has changed quite a lot lately, so I decided to give you an overview of the various Ruby interpreters I use, why I use them and why they also might be interesting for you.

  • Ruby 1.8.6: This is your trusted old Ruby 1.8.6, and while it may not be the most spectacular of the implementations listed here, it’s the most widely used Ruby interpreter around and the current de facto standard of the language.
  • Ruby 1.9.1: Featuring the new bytecode interpreter YARV, this is the newest ‘official’ Ruby version which gives us interesting features like improved speed, block local variables, a new lambda syntax, native threads and some other niceties. So while we are waiting for 1.9.1 to replace 1.8.6 as the standard Ruby interpreter (which probably will take quite a while), my advice is to install it now, play around with it, get used to the new things and help the community in porting gems to 1.9.
  • JRuby: A Java implementation of Ruby, which allows developers to interact with Java classes or define them from within Ruby. It’s fast, it works well, and is a big driving force in the filed of alternative Ruby interpreters. As of now I mostly used JRuby from within “ruby-processing”:, but eventually want to have a look at Monkeybars, which was created in order to simplify the development of Swing apps with JRuby.
  • Rubinius: Here is what the project itself has to say about its relevance for Ruby developers: “A spec driven Ruby implementation. A core library written in Ruby. More first class objects than you can shake a stick at. Super easy to contribute. Low barrier to entry. If you can breathe, you can commit”. So far I only used Rubinius to test some of my own scripts, but eventually would like to find the time to contribute to the project by writing new specs or trying to fix failing ones. I encourage you to do the same, since this is not only a great chance to learn more about Ruby’s internals, but also to help the Ruby community at large.
  • MacRuby: MacRuby is a port of Ruby 1.9 which sits directly on top of OS X core technologies. This means that when you e.g. create an Array, you get an NSMutableArray (see the output of Array.ancestors in macirb: [NSMutableArray, NSArray, Enumerable, NSObject, Kernel]). It also features HotCocoa which provides mappings for common Cocoa methods thus making your MacRuby programs a lot more idiomatic. This together with the interactive way of development provided by macirb make MacRuby an interesting tool not only for Ruby developers, but also for people who want to prototype their OS X apps.
  • Shoes: Some people may be surprised to see Shoes in this list, and technically they are right. Shoes is not a distinct Ruby implementation, but it is a Ruby distribution (which will switch to 1.9.1 in Shoes 3), which lives separated from your other Ruby interpreters, installs gems in its own location and so on. The main thing though is that Shoes provides us with an easy to learn and use, cross-platform GUI toolkit that makes developing in it fun! Granted, it may not be suitable for everything, but with a bit of creativity you can push it quite far. Have a look at the Shoebox to see what people are doing with it.

There are of course other Ruby implementations like Cardinal (a Ruby interpreter for Parrot), IronRuby (Ruby for the .NET environment) and tinyrb (a tiny and fast subset of Ruby), which I won’t describe here in more detail, since I haven’t yet had time to play around with them.

So why should we care? Because the various implementations create a sort of competitive spirit, therefore pushing the limits of Ruby and driving it forward, by making it faster or running on top of other VMs (JRuby, IronRuby), thus letting it leverage a plethora of established technologies and also exposing it to potential new developers. Plus we also got RubySpec, so potential Ruby implementors have a standard to follow.

I hope you enjoyed this post, please leave comments in case you disagree with something or want to add some information!

Rubinius Build Woes

A couple of days ago I decided that I want to play around with Rubinius, so I cloned the source from GitHub and thought I was ready to go. As it turned out that wasn’t quite the case, since my first attempt to build it ended in the second error can see here:

I hopped into #rubinius to find out if more people have this problem, but that doesn’t seem to be the case. At least I learnt that most active develop is going into the ‘stackfull’ branch, so I did a ‘git checkout origin/stackfull’ and gave the build another try, which resulted in the first error message you see above.

Determined not to give up, I decided to try one last time with ‘rake vm:clean’ and ‘rake build:debug’ and to my surprise ended up with a working Rubinius install :-)

1
2

1
2
<span class='line'>$ bin/rbx -v
</span><span class='line'>rubinius 0.11.0-dev (ruby 1.8.6) (f5260b581 12/31/2009) [i686-apple-darwin9.6.0]</span>

That made me somewhat curious, so I tried ‘rake vm:clean’ and ‘rake build’ which now also worked (still can’t build master though). I have to admit that I’m slightly confused, but at least I have a working Rubinius version to play around with for the time being.

Tweet Active Safari Tab From Command Line

Tweeting the URL of the article you are currently reading can be quite cumbersome: start your Twitter client if it isn’t already running, copy the URL, shorten it, paste it into the client…

I therefore wrote a little Ruby script I can call from the command line and which will do all this annoying work for me. As an added bonus you can pass in hashtags as command line parameters. It’s just a quick and dirty hack, feel free to change it according to your needs… :-)

Sinatra Course at RubyLearning Announced!

For a lot of people, Ruby became almost equivalent to Rails, but this doesn’t mean that Rubyists can’t choose from a wider selection of web frameworks. One of them is the microframework Sinatra, for which RubyLearning will start a course on March 21st. Registration is open now, the price is a reasonable US$3 for the first 15 people to sign up, US$4 for the next 25 and finally US$5 for everyone after that. Yours truly will also be a mentor for this course and would be happy to see some of you there!

RubyLearning Git and GitHub Course Review

Today I finished the free Git and GitHub course at RubyLearning and I have to say I’m really happy with it! Since its creation GitHub has become a kind of de facto standard for Ruby-related projects (and probably also for FOSS development in general), so knowing it is quite essential if you want to contribute to any of them. That being said it’s not really hard to get up to speed with the technology, there are good tutorials and other useful sites out there. But if you are just starting out with Git or revision control systems in general, this course is definitely a great way to learn all the basics and much more in a fun and collaborative way! And since the course covers quite a wide range of topics (including automatic gem creation, GH pages, Gists etc.), you’ll probably also find it useful if you already have some previous Git/GitHub experience. The next batch will start on March 28th, make sure you don’t miss it!

Note: Actually the current batch is still going on for 2 more days, but I finished with the eBook today. One of the joys of RubyLearning is that you can do all the courses at your own speed, while still having the motivational and other effects of learning with a group of like-minded people.

Ruby and RubyLearning Keep Me Busy

If you are interested in finally learning Ruby, but don’t have the motivation to do it on your own, you should definitely check out RubyLearning, it’s a great way to learn together with other people but at your own pace.

Right now I’m involved in three different courses as a student and/or mentor:

  • First off there’s the 10th batch of the free Core Ruby Programming course, which started on January 23rd and has over 700 participants, some of whom are really active. I have a GitHub repo for this course, but to avoid spoilers I haven’t committed the exercises from week 5-8 yet. BTW: the next batch will start on March 14th! :)
  • I’m also participating in the paid Ruby Programming with Shoes course which is quite a lot of fun, the repo can be found here.
  • Last but not least there is a free course on Git and GitHub which will start this Saturday (January 21st), so if you are interested in that topic you should sign up right away!

Besides all these courses I’m working on a Shoes tutorial together with ashbb, have my own little Shoes experiments and am reading quite some interesting articles.

Happy Rubying everyone!

Copyright © 2016 - Michael Kohl - Powered by Octopress