Blog - Microsoft .NET, ASP.NET, AJAX and more

Ruby Beauty – Method Naming Conventions

by Damien White 4/12/2010 9:58:26 PM

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Naming conventions can be a good thing providing there is actually some rhyme and reason to them.  We’ve seen a lot of changes with conventions, like Hungarian Notation for example, who’s still using that?  Within the context of .NET, there are defined conventions by Microsoft to which I would say I mostly follow (I still usually prefix private global member variables with an underscore).  The guidelines for .NET are really good, much better than something very arbitrary like Hungarian Notation where no two developers use the same syntax on things. 

There are two method naming conventions in Ruby that I really like.  Within Ruby a method name can end in either a question mark (?) or an exclamation point (!).  Having these conventions makes for nicer syntax.  Let’s look at them in a little more depth More...

Shout it kick it on DotNetKicks.com Bookmark and Share

ActiveSupport Singularize Problem

by Damien White 4/9/2010 9:28:27 PM

I just happened to encounter a problem with the ActiveSupport inflector in my Rails app.  Here’s the problem: words that end in ess seem to be an issue.  For example:

'dress'.singularize # => 'dres'
'business'.singularize # => 'busines'
'address'.singularize # => 'addres'

D'oh, where did that ending 's' go?

Turns out that “Rails has a longstanding policy of not taking further inflector patches. Use an initializer in your application instead,” a quote from Mike Gunderloy.   What exactly does this mean?  Simple…

Within your Rails application, open the config/initializers/inflections.rb file and just add the following:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.singular(/ess$/i, 'ess')
end

All better…

'dress'.singularize # => 'dress'
'business'.singularize # => 'business'
'address'.singularize # => 'address'
Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Optional Parentheses on Method Calls

by Damien White 4/8/2010 8:52:45 PM

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Optional parentheses on method calls are such simple things, but they render some pretty code.  Ruby allows you to omit parentheses on method calls in a lot of cases.  Take the following code:

puts "Hello World"
puts("Hello World")

The two lines are equivalent, yet the first is just cleaner, don’t you think?  Being a diehard C# guy, I do like parentheses, curly braces, and semi-colons, but I’ve got to say Ruby does yield some beautiful code.

In a recent Rails app I used Ryan Bates’ CanCan gem for authorization.  It’s a really nice gem and allows you to define abilities for a user elegantly.  Using CanCan, you can see how nice and clean the code looks.  For example:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

The code flows nicely; what can an admin do? “can manage all.”

If we were required to add parentheses it’s just is a bunch of extra, unneeded noise, and it breaks up the flow:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?()
      can(:manage, :all)
    else
      can(:read, :all)
    end
  end
end

user.admin?() - <gag> right?() :)

Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Dynamically Invoking Methods

by Damien White 4/7/2010 10:03:14 PM

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in others, see my other Ruby posts.

Within the context of Ruby, you can call a method in one of two ways.  The first, and most frequently used, is the standard “dot” notation.  Take the following class:

class SampleClass 
    def say_hello(name) 
        puts "Hello #{name}" 
    end 
end

To then call that method:

obj = SampleClass.new
obj.say_hello('Damien')

This works fine for things you know about, but what if you wanted to call a method dynamically at runtime?  More...

Shout it kick it on DotNetKicks.com Bookmark and Share
Tags:
Categories: Ruby
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Installing Ruby on Rails on Windows

by Damien White 4/6/2010 9:52:00 PM

So it’s not like this doesn’t exist elsewhere on the ‘net, but in case you stumbled upon one of my other Ruby on Rails posts and I caught your interest, here’s how to get started developing Rails apps on Windows.  If you’re a little resistant to try Rails, look how simple the install is.  What are you waiting for?

  1. Head over to http://rubyinstaller.org/ and install Ruby executable from there.  I usually install both 1.8.7 (current release Ruby 1.8.7-p249 (RC2)) and 1.9.1 (current release Ruby 1.9.1-p378 (RC2)) and use pik to switch between the two with ease.  I also have the RubyInstaller set my path variable to include the install path for Ruby 1.8.7.  The RubyInstaller includes Ruby and RubyGems.  If you are not familiar with RubyGems, a gem is a Ruby package.  Accessing and using a gem is a simple gem install command, which you will see in a moment. 

  2. Fire up cmd on your machine and type ruby -v.  You should see:
    D:\>ruby -v
    ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]
    If you don't, check your PATH variable and make sure you have an entry for your installation path (typically C:\Ruby\bin). Note your version listing may be different than mine.

  3. You may want to update RubyGems.  The RubyInstaller (at least at the time of writing) includes v1.3.5.  To do this, use the command: gem update --system
    The current version as of this post is v1.3.6.  You can check your gem version with the command gem -v

  4. Now it’s time to install Rails, which thanks to gems is as easy as running the command: gem install rails.  Note that installing Rails takes a bit of time. After Rails is done, use the command rails -v to verify it installed correctly. 

  5. After Rails is done, you should install a database.  The easiest to get going with is SQLite.  You’ll need to download the EXE and the DLLs for SQLite (from here: http://sqlite.org/download.html).  Look under the Precompiled Binaries For Windows section.  You’ll have three files after downloading the two zip files (sqlite-3 and sqlitedll).  Put the sqlite3.dll, sqlite3.def, and sqlite3.exe in your Ruby\bin directory (typically C:\Ruby\bin).  Once you’ve done that you’ll need to install the sqlite3-ruby gem, which is just: gem install sqlite3-ruby

  6. That’s it!  Now you can start working with Rails.  Let’s make a simple note taking application… More...
Shout it kick it on DotNetKicks.com Bookmark and Share