Code Kōans

Recently (albeit it seems a few years after a lot of my fellow developers) I’ve dug into kōans. A kōan (Chinese 公案, Korean 공안, I had to put the symbols, but I just find them awesome 🙂 ) is a fundamental part of the history and lore of ZenBuddhism. It consists of a storydialogue, question, or statement, the meaning of which cannot be understood by rational thinking but may be accessible through intuition. The classic example is “Two hands clap and there is a sound; what is the sound of one hand?

The first set of koans I checked out (thanks to @Ang3lfir3) were the Clojure Kōans. I’ll admit, I had a slightly higher priority with Ruby so I grabbed the Ruby Kōans. I’ll get to the Clojure ones soon, as Clojure is a great language to work through and derive meaning through intuition. With that little intro, I’ll dive in…

The first file is absurdly easy, basically making sure reality exists in that true is true and true is not false. These seem pretty straight forward. When I got into the subsequent koan test files though, things started to get interesting. The Ruby Language itself started to show itself and how it actually works. When working through the koans, you’re actually working with real Ruby Unit Tests and code files. In my descriptions below I’ve cut those files up so I can comment in between each bit of code. I will however format each bit of code properly for readability. Cheers!

[sourcecode language=”ruby”]
class AboutObjects < EdgeCase::Koan
def test_everything_is_an_object
assert_equal true, 1.is_a?(Object)
assert_equal true, 1.5.is_a?(Object)
assert_equal true, "string".is_a?(Object)
assert_equal true, nil.is_a?(Object)
assert_equal true, Object.is_a?(Object)
end
[/sourcecode]

After running through this test, it’s a little obvious the point. Everything is an object. Get it? :O

[sourcecode language=”ruby”]
def test_objects_can_be_converted_to_strings
assert_equal "123", 123.to_s
assert_equal "", nil.to_s
end
[/sourcecode]

Ok, so I kind of feel that to_s is a bit silly for a method name, but whatever. I’m also assuming at this point that “nil” is a reserved object or something. I’m not sure what the assumption of nil is though, since it is an empty string, but sort of null, but not null, but maybe no value? I’ll just have to look this bit up later.

I did a search to dig this up and am even more confused now. I found this entry first, which would lead me to believe that nil is also a method on an object, but an object itself? Ok, I’ll read more later and move on to the next tests, maybe intuition will strike and I’ll all of a sudden realize what I’m working with here.

[sourcecode language=”ruby”]
def test_objects_can_be_inspected
assert_equal "123", 123.inspect
assert_equal "nil", nil.inspect
end
[/sourcecode]

The inspect method, seems simple enough. The method is or has a sort of default print out of what it is called under “inspection”. Ok, next…

[sourcecode language=”ruby”]
def test_every_object_has_an_id
obj = Object.new
assert_equal Fixnum, obj.object_id.class
end
[/sourcecode]

This is simple enough, every object gets an ID, whatever it may be. I’d bet it is unique, not that this test proves that. …I do wonder though, what exactly is Fixnum?

[sourcecode language=”ruby”]
def test_every_object_has_different_id
obj = Object.new
another_obj = Object.new
assert_equal true, obj.object_id != another_obj.object_id
end
[/sourcecode]

Ok, as I suspected, object IDs are unique. Got it.

[sourcecode language=”ruby”]
def test_some_system_objects_always_have_the_same_id
assert_equal 0, false.object_id
assert_equal 2, true.object_id
assert_equal 4, nil.object_id
end
[/sourcecode]

Umm. Yeah. Ok. That makes sense sort of. I’m now more confused about nil. It is 4, or null, or part of an enumeration? Who knows, who could tell from this test. I’ve got it though, false is zero, and two is true for some reason? I want to know what happened to one, which I suppose, is another thing I’ll have to intuit later. Onward!

[sourcecode language=”ruby”]
def test_small_integers_have_fixed_ids
assert_equal 1, 0.object_id
assert_equal 3, 1.object_id
assert_equal 5, 2.object_id
assert_equal 7, 3.object_id
assert_equal 9, 4.object_id
assert_equal 11, 5.object_id
assert_equal 13, 6.object_id
assert_equal 201, 100.object_id
# THINK ABOUT IT: 2x the number +1 == WTF? Why choose that?
# What pattern do the object IDs for small integers follow?
end
[/sourcecode]

Ok, I wanted to verify what the pattern was, so I actually added some asserts to this test. Hmmm, it appears, after adding the tests, that there is this nifty patter but I just can’t put my finger on it! 😉 (in case it isn’t evident, I’m being sarcastic here)

[sourcecode language=”ruby”]
def test_clone_creates_a_different_object
obj = Object.new
copy = obj.clone
assert_equal true, obj != copy
assert_equal true, obj.object_id != copy.object_id
end
end
[/sourcecode]

Ok, verified that object IDs stay unique upon creation or even cloning of new objects. This is an important thing to realize and understand within the overall design of Ruby. Enough for this short work through, more later, and again go and check out the Ruby Kōans yourself, they’re a lot of fun to work through.

The Bus Ride Home, Sinatra Singing on OS-X

This is just a quick getting started I walked through to get Sinatra going on OS-X.

[sourcecode language=”bash”]
sudo gem install sinatra
sudo gem install shotgun
sudo gem install haml
sudo gem install unicorn
[/sourcecode]

After installing these things I created a new folder in a Rubymine Project.

New Empty Project in Rubymine
New Empty Project in Rubymine

I added a Ruby Class named SinatraSingsOsx to the project. I then realized that Rubymine did not detect that I had installed Sinatra, so I did a quick find in Rubymine to find and get it synced/installed so that Rubymine was aware of the gems.

Rubymine IDE
Rubymine IDE

Using the gem quick install in Rubymine I chose the documentation and dependent gems.

Rubymine Gem Quick Install
Rubymine Gem Quick Install

Once those were synced up I added the following Ruby Code to the file I created in the project.

[sourcecode language=”ruby”]
require ‘rubygems’
require ‘sinatra’

get ‘/’ do
"Verified: Server is up and running!"
end
[/sourcecode]

I then right clicked on the ruby file and selected the “Run the sinatra_singing_osx” which starts up the webrick web server and such.

Rubymine Run Page Dialog
Rubymine Run Page Dialog

Then navigate to http://localhost:4567 and you’ll see the results of the code.

Sinatra Web Application Running in Chrome
Sinatra Web Application Running in Chrome

Now that I’ve run the application in Rubymine I wanted to do the same with Shotgun using the same Ruby code. The first thing I needed to do was stop the server running that Rubymine had launched. If you look at the bottom of the IDE you’ll see the window displaying the current server status.

Web Server Executing
Web Server Executing

On the left hand side of this status window click on the large red button. This will stop the service. To verify just scroll to the bottom of this status window and you will see the message “Process finished with exit code 1“.

Now that I had the server cleaned up and stopped, I started a terminal again and navigated to the directory that the *.rb file is in that I want to start (host/run/execute, I’m not really sure what the appropriate word would really be at this point). So once at the appropriate path (machineName:SinatraSingingOSX adron$) I typed:

[sourcecode language=”bash”]
shotgun -p 4567 sinatra_singing_osx.rb
[/sourcecode]

Once this executes the same server that Rubymine uses, the WEBrick, will startup the application again. I launched the browser to assure this ran as expected and got the same results as if running it through Rubymine. I guess, it all really depends, want to just start building and checking things out or want to build with an IDE like Rubymine. It all really boils down to taste and what you’re working with. I’ve been going back and forth between pure text editor and IDE just to familiarize myself with both avenues of using Rails and coding with Ruby.

After the verification I hit Ctrl + C to stop the web server. This basically works on the msysgit in Windows, on OS-X, or on whatever distro of Linux you have running. The cool thing is, so does Rubymine. I figured if I was going to work toward familiarization of an IDE and using the terminal I’d use commands and IDEs that would work on whatever OS I’m running. It’s all good synergies that way. 🙂

I wanted to keep this entry pretty short. With that in mind I simply logged what I worked on while en route home on the bus one evening. So on the next bus ride I’m going to tackle getting some specific notes with Sinatra, getting this all pushed into Heroku (of course, I’m all about the Cloud, you didn’t think I’d put it up on a shared host did you!) Until then, chow.

The Non-Microsoft Realm, Collecting Rubies Part II

Just a quick Friday night entry with helpful tidbits… cheers!

Part I of the Collecting Rubies Series.

RVM Gemsets

A few steps when setting up Gemsets with RVM. With a few other commands that can often be helpful.

Functionality by line:

  1. Get a list of the current available gemsets.
  2. Creates a gemset called theNameOfTheSiteToCreate.
  3. Lists the name of the current gemset.
  4. Delete the gemset named theNameOfTheSiteToCreate.
  5. This lists the current Ruby Version selected.

[sourcecode language=”bash”]
rvm gemset list
rvm gemset create theNameOfTheSiteToCreate
rvm gemset name
rvm gemset delete theNameOfTheSiteToCreate
rvm list
[/sourcecode]

Git-flow

Check out the git-flow github account.

To install:

[sourcecode language=”bash”]
wget –no-check-certificate -q -O – https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | sudo sh
[/sourcecode]

…then just type git flow and you’ll see this prompt, which provides some functionality that is usable right away…

[sourcecode language=”bash”]
usage: git flow

Available subcommands are:
init Initialize a new git repo with support for the branching model.
feature Manage your feature branches.
release Manage your release branches.
hotfix Manage your hotfix branches.
support Manage your support branches.
version Shows version information.
[/sourcecode]

SQL Lite 3

Check out the sqlite3 site.

To install:

[sourcecode language=”bash”]
sudo apt-get install sqlite3 libsqlite3-dev
[/sourcecode]

My Day @ Seattle Mobile Hackathon (#mobileappSEA) The Morning

I arrived around 8:20am “ish”. @juliaferraioli & cohort were registering people. The @AWSstartups crew was there including @Jeffbarr, also @shanley, @Alex_donn, @JamesPearce, and others were already getting things put together. I decided to happily plunk down in a chair and get going. I had zero plan, but only a single goal. Make a web app that isn’t device proprietary, but is mobile centric, and get it live on the web.

My initial dev machine load that I intended to get this accomplished with I posted yesterday.  So here’s my first push…

First a quick run thru of WebStorm and RubyMine IDEs.

{Edited this part on Apr 20th to provider more information regarding Java Installation} If you’re on Ubuntu, open up the Synaptic Package Manager and search for java6, which should bring up the sun-java6-jre.  Mark it for installation and apply so that the JDK will install with the required components.  If the Java 6 isn’t available in the Synaptic Package Manager open up the Settings -> Repositories, and then the Software Sources Dialog will appear.  Click on the Other Software tab and select Canonical Partners.  Close the dialog and Reload the packages.  When a search is done now, the sun-java6-jdk should be available.

Next edit the profile at /etc/profile by entering the command:

[sourcecode language=”bash”]
sudo gedit /etc/profile
[/sourcecode]

Add the following to the bottom of the file:

[sourcecode language=”bash”]
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export JDK_HOME=$JAVA_HOME
export RUBYMINE_JDK=$JAVA_HOME
[/sourcecode]

After this is done, restart your X-Windows/Gnome Instance or simply just reboot. I don’t really like to suggest rebooting since it usually isn’t needed with Linux. 😉

Unzip both packages downloaded from Jetbrains.

[sourcecode language=”bash”]
tar -xvzf WebStorm-2.0.1.tar.gz
tar -xvzf RubyMine-3.1.1.tar.gz
[/sourcecode]

You may want to put your new applications into a specific directory. I placed mine in a folder I made within my user folder called apps. Just a little easier to keep up with things that way.

Creating a Launcher
Creating a Launcher

Once these are unzipped right click on your desktop and choose “Create Launcher…”. Click on the Browser to bring up a folder navigation dialog, find the bin directory of the app your creating a launcher for. In my case it is /home/adron/apps/WebStorm-103.243/bin/ which has the WebStorm App Files needed for the Launcher. Find the WebStorm.sh file and select it.

Once the app file is selected then check ok. For a more step by step, check out Tomi’s Blog Entry.  Do the same for both apps.

With both of those apps, simply launch the IDE and enter your key (or select 30 day trial) and you’re up and running.

RubyMine Startup (click for full size image)
RubyMine Startup (click for full size image)

With the awesome Jetbrains IDEs installed I was really ready to dive into something.  Next I went straight for a new project in RubyMine.  File -> New or just click on the Create New Project on the main RubyMine Startup Screen.

New Project Dialog
New Project Dialog

Pick your name, I’ve decided to go for some sushi with a project type of Rails application, and click enter.  The next dialog that comes up asks some standard settings information options.

Rails Settings
Rails Settings

If you click on the ellipsis button to the right of the Rails Version, a prompt will come up to select which rails version you want to use.  Ruby Mine will then install that version if it isn’t installed already.

Install Rails Version
Install Rails Version

I decided to check “Generate RDoc and ri documentation” also, and then clicked on Install.

Installing Rails Version
Installing Rails Version

Once the options and settings are made for the new project, click to create the project.  RubyMine will work for a few seconds, maybe 20 or 30 on slower machines, and eventually the IDE will display with the newly created project.  When I created it the first time I was informed I was missing some gems.

RubyMine Project (Click for larger image)
RubyMine Project (Click for larger image)

I clicked on the More… option near the Install and attach missing gems using bundler… warning to get the additional gems I needed.  This brings up a dialog specific to maintaining the Ruby SDK and Gems.

Settings (Click for larger image)
Settings (Click for larger image)

I clicked on Install Gems and found the the sqllite3 and sqllite3-ruby gems to install.  I clicked on install and then apply and OK (ok, old habit, not sure WHY apply gets put on these dialogs) on the settings screen.  Once done getting those last bits installed click on the Development: SomeSushi (or whatever you’ve named the project) and select Edit Configurations.

Development: SomeSushi
Development: SomeSushi

On the Edit Configurations Dialog screen check Run Browser from the bottom of the options and click on OK.  Then click on run button (the green play button on the button bar) to run the Ruby on Rails Web Application.  Within a second or two you’ll see the default Ruby on Rails + RubyMine Run the site in the browser.

RubyMine Ruby on Rails Default Web App
RubyMine Ruby on Rails Default Web App

At this point in the day I took a break, ate lunch, talked to @AWSstartups crew @rodica & @Jeffbarr.  Introduced myself formally in person to @shanley and chit chatted about our respective efforts with @lazycoder and @juliaferraioli.  The food was great, as in, it was actually really good.  Not some random junk.  Super tender chicken with spaghetti sauce and little round funny shaped pastas (I’m sure there is an appropriate name for em’).  I was honestly impressed by the food brought in.

After lunch I dived back in for the next phase of development.  For that though, I have another blog entry coming…  (stay tuned).

The Non-Microsoft Realm, Collecting Rubies Part I

Years ago I worked with Linux (Redhat 3.2, SUSE, Gentoo, and Slackware among others) and also played around with PHP.  I’ve never really gotten too keen in PHP, mainly because it just seemed sort of a mess.  But when I needed something to get the job done and didn’t want to wait on the bureaucratic nonsense of corporate governance, PHP was there for me, albeit with a headache to follow.

However my interest in Linux and alternate development stacks, being in Southern Mississippi left me pretty much one option if I wanted to get into software development:  Microsoft’s .NET Framework.

So I learned that, and work with it regularly, and I dig it.  Most of the time.  It serves its purpose.  I have however wanted more, so I’ve started digging into other things again.  This is something I do frequently, and in my not so humble opinion think any serious developer should also dive into regularly.  Step outside of your comfort zone and try other things out, often this helps expand both options (such as .NET and Java, or Ruby on Rails, or whatever).

Recently I’ve decided I’m diving head long straight into Ruby on Rails.  I’m working on a new personal project and have determined I’m not going to use one lick of Microsoft Software (you can read in other motivations if you want to).  This project will be 100% Linux (maybe some Apple Gear & Bits) and Ruby on Rails.  I’ve not determined much beyond that.  Database or data store, TBD, graphics, TBD, and other such are all TBD.

Ubuntu Download
Ubuntu Download

With that, this is how & where I started from the ground up.

  1. I snagged Ubuntu and loaded it (yes, it does dual boot on my machine that runs Windows 7).  http://www.ubuntu.com/
  2. Next I went straight to TekPub and started downloading some of the Ruby on Rails Video Tutorials the crew over there has put together.  I purchased a membership because they’ve put out a lot of great material on everything from .NET, Entity Framework, NHibernate, Good SOLID Code, to Linux and Ruby on Rails.  I like the efforts, the teaching style, and they’re rather entertaining.  If you really don’t want to shell out any cash, there are other videos available on the Ruby on Rails Site.
  3. I hit a few snags on working with Ubuntu, mainly because I had not touched a Linux UI System in years.  The first two issues were;  1) How do I change the screen settings to not duplicate my dual monitors and 2) How do I take screenshots.  < Click on the respective issue to see what page I found to resolve the issue.  Yes, they were both that easy to resolve.  Literally first click links of Google results.  Very cool.  🙂
  4. The next issue I ran into was related to playing the TekPub Videos.  Using the Synaptic Package Manager however I was able to download all the additional codecs I needed.  I have to say, since the UX was vastly superior to Microsoft’s Windows Media Player (or whatever it’s called these days) I was able to get the codecs without much manual searching.  I just clicked ok, the package manager popped up, found what I needed, and I clicked OK.  This experience reminds me more of Apple’s OS-X than Windows strangely enough.
  5. With the TekPub Videos now viewable I downloaded the first Rails 3 Video.

While watching the video, which includes great coverage of what Rails 3 is all about, I worked through the following bits.  For these I also left the GUI Synaptic Package Manager and went with the command line, simply, it is just easier and faster.

Git

This was stupid simple.

[sourcecode language=”bash”]
sudo apt-get install git git-gui git-doc gitk
[/sourcecode]

Build Bits

Some important parts for building Ruby.  Yeah, this is kind of a kicker, super long, crazy command, best to copy and paste.  😉

[sourcecode language=”bash”]
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
[/sourcecode]

Curl

Reached out and got some curl.

[sourcecode language=”bash”]
sudo apt-get install curl git-core
[/sourcecode]

RVM (Ruby Version Manager)

Bash some Ruby Version Manager.  Note I put in “cd~” just as a reminder to get to your user directory.  Most likely, after installing RVM you’d already be in that directory.

[sourcecode language=”bash”]
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
cd ~
gedit .bashrc
[/sourcecode]

At the very bottom of the .bashrc file add the following text.

[sourcecode language=”bash”]
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
[/sourcecode]

Once you’ve done that, close the terminal down and then reopen it so that all the paths and such can load.

[sourcecode language=”bash”]
sudo apt-get install libruby1.9 zlib1g-dev libssl-dev libreadline5-dev build-essential
[/sourcecode]

Ruby 1.9.2 Bits

Now comes the exciting part, getting Ruby loaded up good and proper.

[sourcecode language=”bash”]
rvm install 1.9.2 –with-zlib-dir=~/.rvm/usr
rvm use 1.9.2 –default
[/sourcecode]

Now if you check with “ruby -v” the ruby 1.9.2p0 version will be default.

Setting Ruby 1.9.2 as Default
Setting Ruby 1.9.2 as Default

Collecting Gems

Now you’ll need some bits, in this case, called gems.  This is one of the very powerful parts of Ruby on Rails.  Beware, these bits have glorious AWESOME all over em’.

[sourcecode language=”bash”]
gem list
gem install rails
[/sourcecode]

The gem list command should list what is currently installed. Initially, only the rake gem is installed.  Once you execute gem install rails, then do another gem list, you’ll have a whole set of gems installed.

Checkup On Your Software

These are just a few of the commands that are helpful in identifying what is installed now. Try em’ out to get a view of what is available.

[sourcecode language=”bash”]
rvm gemset name
gem list
[/sourcecode]