Tuesday, January 16, 2007

Jedi Rubyness

I love Ruby books. They're fun to read. Being that there's a contest going on for blogging about Ruby on Rails, I can't pass this up. Apparently I have a chance to win a Ruby book by blogging about how Ruby on Rails has made me a better programmer. Awesome!

This is going to read a little like an 'epic' type of thing - it will involve love, hate, frustration, elation, and the occasional video game / movie reference (alright the whole thing is a movie), but I hope after reading this you will feel as inspired as I did writing about my path to Jedi Rubyness and Rails. Think of it as a quest. We all love quests - Star Wars, the Legend of Zelda, etc (ok never mind I'm just dating myself). This is a quest about getting better at programming because of Ruby on Rails!

Jedi Rubyness:

Me : the kid who happens upon a cool thing like the Force, or a lightsaber, or in this case Ruby On Rails

Java the Hutt
, or JavaVader : you know, in a sense it could be the villain, you'll see why!

Obi-Wan Kenobi
: David A. Black (only he doesn't get killed off by Java)

A long time ago (past year) in a galaxy far, far away, (New Jersey), I wasn't doing much of anything as far as programming. I occasionally dabbled in front end web design with Flash and HTML, but other than that I never really utilized anything in a "pure programming" sense.

One of my clients (one of those princesses, as long as they're played by Natalie Portman), a martial arts instructor needed a site for his school asking for some nifty looking GUIs and music and all. I was able to create something very cool and showy with Flash, but something was missing (Jar Jar Binks). He wished to have a little more control of the website, maybe wanting to update news or allow other users to create accounts and login and such. My knowledge of front end design would have allowed me to create a great looking front page for everyone to come in and see some links to those actions, but other than that I was dead in the water in granting my client's request (I couldn't use the Force yet).

So, remembering my Java and JSP days back in database development class, I attempted by my lonesome to roll together a small database backed website that could allow some users to post messages or create something new on the site themselves. Pain ensued.

(note: I don't know about you, but obviously some web developers out there will consider installing a Java based web dev stack childs play. Keep in mind I was a web dev newbie, with my prior knowledge in HTML, CSS, Flash, and MAYBE some java and C on my local machine, nothing ever associated with web dev. Building a Java web dev stack was a nightmare for me).

Java the Hutt: Ho, ho, ho . . .

JavaVader: If you only knew the POWER of Struts, Tomcat, Javabeans, Swing, Hibernate, Ant, Maven, and J2EE . . . *wooooosh*

No, I wasn't listing what I attempted to install. I was listing the topics I saw when I went to my local bookstore to research the matter. First thoughts that came into my mind (beyond the OH !@#$) was INTIMIDATION (Fear leads to hate, hate leads to anger, anger is the result of confusing base installations)

Not wanting to back down from a challenge, I started reading through some example code, hoping to find a chapter that would tell (in minimal steps) how to get maybe a shopping cart online or a blog. I had to put the book down as my mind couldn't seem to process all the code that was required.

Plus, for some odd reason, I was getting a bad feeling about this.

That was a discouraging experience for me. For someone wanting to "migrate" (get it? get it?!? ahh nevermind . . .) from front to middle to backend web development, it seemed like I would be spending so much more time studying and configuring rather than applying or creating.

Sometime later in the month, I went back to see if I couldn't grasp any other technologies that would help me out in my quest:

"Ok, we got Perl, PHP, hmm next to that is Python and . . . wait a second . . . Ruby for Rails? I've HEARD of this Ruby thing through a friend who's a Linux sysadmin and he said it would be cool for me to learn Ruby, but I never had the chance to research the matter until . . . now . . . let's see what this book has to say".

I don't really recall if my reactions were of the jovial type (SCORE!), or if I even broke a smile (CHEESE!). What I do remember is that one of the reoccurring thoughts (besides "Use the Force") in my head was "I can DO this" and "this looks like fun". Here, in one chapter, David A. Black walks you through setting up a music store application (bare bones of course). Of course I didn't know what :attr_accessor did at the time, but the rest of the code seemed to read like a story.

I walked home that day with Agile Web Development with Rails and Ruby for Rails. I figured that the Agile book would be good to get me started in just coding away while the Ruby for Rails could be a supplement in learning (and my first Rails lightsaber). I was actually eager to give this a try.

Like any toddler learning to walk, I crawled, drooled, stumbled, tripped, fell, cried a little, but then started to waddle (holding onto something to keep my balance). I was able to apt-get Ruby (thank you Debian packages!), install Rails, get my MySQL database talking to it, and almost cheered when I saw the all-too-familiar Welcome aboard, you're riding the Rails! page. THAT was easy.

Scaffolding was the early childhood magician for me - lots of great autogenerated magic that awed me when I knew so little about it, then later upon learning exposing all the tricks and semi-disappointed me (but still much appreciated as training wheels!). I felt like a Ruby / Rails padawan. Not even a padawan, just some guy who has been exposed to something that I definitely knew had so much potential but I didn't know what to do with it. Enter the Ruby / Rails community.

It didn't take long to find a local Ruby hackers meeting. My first exposure to the Ruby community in person was at a New York City Ruby Hacker meeting in . . . NYC. Prior to this I had never been to a programmers' meeting or a user group. I must say I'm glad this was my first experience and sorry that it will probably spoil me from now on.

The people I met were very friendly and extremely knowledgeable and very willing to walk me through some basic Ruby bootstrapping. Ruby has bought together a community I'm proud to be a part of and support (now I could mention specific people I've met such as Francis Hwang, Amy Hoy, Ryan Davis and the sort and try to match another Star Wars character to them, but to me they will just remain kick-ass Ruby Jedi)

Just being around the community whether it be live at meetings or on IRC changed my programming habits. I read programming books voraciously, always wanting to know more. I changed my coding 'style' to a more test-driven approach thanks to ZenTest / Autotest. I was able to run code on the fly with IRB, and somehow understood self-referential has_and_belongs_to_many relationships.

Were there any aspects of Ruby on Rails that specifically made me a 'better programmer'? I would say all the improvements in me were subtle. I'm still learning, but no other language has me wanting to learn MORE about it. I believe the fact that it sparked my passion for learning more about programming, testing, deployment, etc. makes me a better programmer than the average one. I believe the fact that I ENJOY the language I program in makes me a better programmer.

So yes, there may be a host of other programmers out there who can do (n) variations of a quick sort algorithm, but I would take a bet that there aren't many that enjoy the language they program in, wish to contribute to it, and even formed a user group in their hometown just so they can get more of a Ruby fix each month (shameless plug: Go New Jersey Ruby!)

Epilogue: I guess there could have been some epic battle but I don't know what to use for Star Destroyers or Ewoks. All I know is that the path to Jedi Rubyness via Rails is a FUN one and I encourage anyone with a creative spark in their eye to take that path!

Sunday, December 17, 2006

Let's Get This Started!

One of the many reasons I started to program in Ruby was the sheer beauty of the code. You can get lots done with minimal coding and still have readable, maintainable code. But instead of me jabbering on, why don't we get things rolling, shall we?

Are You Installed???

[Brian and Peter are putting a crib together.]

Brian: Okay, insert rod support A into slot B.

Peter: That's what...

Brian: If you say "that's what she said" one more time, I am gonna pop you.

-Family Guy

I develop my Ruby and Rails applications on a Dell laptop running Ubuntu Edgy. You can still follow along if you are on a Windows or Mac machine, but the screenshots will be different. There are specific installation packages you will have to, um, install, for your platform. Check the end of this article for specific links for your platform.

For you to start ruling the world with Rails, you will need the following:

  • Ruby
  • RubyGems
  • Rails framework
  • MySQL database (can also run on DB2, Oracle, Postgres, SQL Server, and SQLite)

Installing Ruby:
Open a console window and type in the following:

# ruby -v

Most likely you will get a prompt telling you that ruby is an unknown command. However, if you get something like this:


ruby 1.8.5 (2006-08-25) [i686-linux]

. . . then someone loves you upstairs and has already installed Ruby on your distro! For now, let's go over installing Ruby on Ubuntu:


  • Use the apt-get command. For Debian based distros (including Ubuntu!) you can simply type in the following command:

    # apt-get install ruby
    Note: you may have to sudo the apt-get. You will need to belong to the sudoers list in order to execute this. You can find the list in /etc/sudoers. Use the visudo command to add yourself to the list.

  • Compile Ruby from source. This will apply to all Linux distros and will be a little bit more of a heavyweight install as opposed to package managers. The tradeoff is you will have the latest stable version of Ruby. Being that you are on a badass Ubuntu system, let's compile from source and get the full taste of building Ruby just for us!

Before we get started, let's get some tools installed to make everything flow smoothly:


# sudo apt-get install build-essential zlib1g-dev

The build-essential package will provide you with make and the Gnu Compiler Collection (GCC) which is used to compile things from source. Think of them as your screwdrivers, hammers, and nails that we'll use to build our awesome Rails framework (actually, we should think of those tools as hacksaws since we are in a sense hacking, but I digress . . .)

Of course now we could visit our friends at www.ruby-lang.org and get the latest Ruby release, but since you're here already, why not a link? :-)


GET LATEST RUBY HERE!
Note: At the time of this article that was the latest stable release. You may have to visit the site in case I'm a little outdated!



Now that you have the latest Ruby downloaded, let's extract it into a temporary directory:


# tar zxvf ruby-version.tar.gz

You then change to the directory where you extracted the source and run the following commands so you can compile and install Ruby . . .

# ./configure
# make
# sudo make install

Note: Remember the sudo requirements from above. Depending on the speed of your system, the last two commands may take a little while. I recommend a quick game on your Nintendo DS or grabbing a snack to eat at those points.

Are We There Yet????


If everything went well, you should be there. If you get error messages in your make or sudo make install commands, your best bet will be googling those errors. Remember to include the name of your distribution in the search path as well as the actual error message.


However, if you're one of the badass Linux users we all know, fear, and love, then you should be able to type in this and get the following similar output:


# ruby -v
=> ruby 1.8.5 (2006-08-25) [i686-linux]


Congratulation, you now have the latest version of Ruby installed on your system!

Should be easy coasting from here. Our next task is to install RubyGems, which is Ruby's own package manager, similar to Ubuntu's apt-get. You can find the latest gems version over at http://rubyforge.org under the RubyGems project's homepage, or you can just follow the link:



GET THE LATEST RUBYGEMS HERE! ! !
Note: This was the latest version at the time of this article, you may have to go to rubyforge.org if this article is dated.


Now that you have the source file, the installation will be similar to Ruby's - first, extract the package to a temporary directory:


# tar xzvf rubygems-version.tgz

Then change to the temporary directory and - nope! No make or make install here, you have Ruby now! Check this out:


# sudo ruby setup.rb

You should see the following similar results if all goes well:


=> Successfully built RubyGem
=> Name: sources
=> Version: 0.0.1
=> File: sources-0.0.1.gem

Now we verify that all went well:


# gem -v
=> 0.9.0

Booya!



Install Ruby On Rails

Now that we have RubyGems installed, let's wrap things up here and get Ruby On Rails to stop your salivating!


# sudo gem install rails --include-dependencies

After a brief wait as the gems package manager "phones home", you will see a nice little flurry of output letting you know that Rails has been successfully installed. Let's make sure!


# rails -v
=> Rails 1.1.6

Booya again! Aside from your database, you've got a vanilla installation of Rails, read to rock!



But it didn't work! What do I do now???
Nothing, all hope is lost! Just kidding - read over the installation steps and be sure you had all the required build dependencies. Make sure you have sudo working correctly. Once again, if all else fails, be sure to google those error messages, I'm sure your error might have been discovered by someone else and they could help!


MySQL, YourSQL, Everbody's SQL!


For you Ubuntu users out there, you're in luck. The current Ubuntu Edgy 6.10 release should have MySQL 5 in its package manager, giving you all you need to satisfy the database requirement for Rails. Simply enter the following command:


# sudo apt-get install mysql-server

If all goes well the server will be installed and then run as a background process. Let's verify:


# mysql -u root
=> Welcome to the MySQL monitor. Commands end \
=> with ; or \g
=> Your MySQL connection id is 8 to server \
=> version: 5.0.24a-Debian_9-log

=> Type 'help;' or '\h' for help. Type '\c' \
=> to clear the buffer.

=> mysql>

Great! Now let's make sure that Rails can take full advantage of your database!



Let's go for a drive!

You will need to install the MySQL database driver to communicate with Ruby. Prerequisites to this will be the developmental libraries for MySQL which we can find with the following command:


# apt-cache search libmysqlclient

Look for the latest mysql database client library. Since we have MySQL5, the correct version will be:


# sudo apt-get install libmysqlclient14-dev

Note: If you have MySQL4, then you would install libmysqlclient12-dev.



Last, let's install the driver by using gems:


# sudo gem install mysql
=> Successfully installed mysql-2.7


Give yourself a pat on the back, go get a beer, and call your friends over. You have a vanilla installation of Ruby On Rails, ready to rock all your web development needs. Hope this installation tutorial gets you going! Next time - introducing the world to your new Ruby / Rails installation!



If you are running Windows or MacOS, please go here to find out what steps are needed to get Rails onto your system!

Saturday, December 16, 2006

Great Time To Ride the Rails!

Believe it or not, my Rails journey started with a Linux Auditor Live DVD. On that DVD was the open-source exploit framework Metasploit which was written in Perl at the time. Curious, I visited the site, read through the documentation, and found out that the whole project was being redone in Ruby.

What struck me about the developer's documentation was how easy it was to read. The code read pretty much like a book and I could visually and conceptually link everything together in my mind.

Ruby code sure was clear.

So then I decided to go browsing at my local Barnes & Nobles for some books to further fuel the fire. After flipping through the very limited selection (at the time) of Ruby books, I came home with Agile Web Development with Rails and Ruby For Rails. What followed afterwards was a blur.

Literally within the same day I had a database-backed web site where I could create a blog or shopping cart and keep orders and transactions. Even though they were both VERY rough and in development mode, it said a lot for someone like me who had mostly front end web design experience. The process got me psyched and made me want to learn more Ruby and Rails.



Ruby For You (and me)!


Since then I've participated in and joined the NY Ruby Brigade and I'm also very proud to be a part of the New Jersey Ruby Group. After much coding and researching and learning and studying, I've realized something . . .

My mind is having trouble keeping up with my passion for learning everything (as corny as that sounds)! Quite literally, I think the biggest problem I'm having right now is that there aren't enough hours in the day. Plus, for some weird reason, we have to sleep.

Well, being that I'm no superman, I obviously forget. Or, there are times while I'm coding that I just can't seem to recall a certain method, or regex, or what relationship to put in my models.



Mission Statement


This [blog] will be my savior. My martial arts instructor used to tell us to keep a fighter's journal of all the techniques and philosophy we absorb during our time training, because there is such a wealth of information to be absorbed. The same applies to learning Ruby, and that is where the intention of all the future blog articles will lie:

This blog is mainly for you and me. For me, it's a record of all my do's and don'ts, heartaches and joys of learning and programming in Ruby. For you, it's a chance for any Ruby or Rails or even coding newbie to join forces with me, still a relative newbie, in the fast growing world of Ruby and Rails development.

For many of you, you have just heard of this Rails thing. Or maybe you are one of the few like me who had just stumbled upon some Ruby code and would like to see some more. Let me tell you something - it's an awesome time to be a Ruby n00b.

I will make it my job to make sure your journey through learning Ruby will not only be one of the most rewarding experiences but also fun.

Welcome to the world of Ruby / Rails n00bness, we're all in this together!!!

Feel free to comment me with questions. I frequent the #nyc.rb, #rubyonrails, and #ruby-lang channels on irc.freenode.net under the nickname hookET. Looking forward to hearing from y'all!

Next up . . . getting a vanilla installation of Ruby on Rails onto your system!