Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, May 31, 2012

Ruby Koans

If anyone is learning Ruby, and wants to become more familiar with various nuances, go download the Ruby Koans.

There are about two dozen files, each discussing a specific topic. You go through each file, looking for __ segments and replace them with statements which would result in true assertions.

After completing a few, you can run ruby path_to_enlightenment.rb and you get the following prompt:

user:~/koans$ ruby path_to_enlightenment.rb 
AboutNil#test_nil_is_an_object has expanded your awareness.
AboutNil#test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil has 
expanded your awareness.
AboutNil#test_nil_has_a_few_methods_defined_on_it has expanded your awareness.
AboutObjects#test_everything_is_an_object has damaged your karma.

The Master says:
  You have not yet reached enlightenment.
  You are progressing. Excellent. 8 completed.

The answers you seek...
  <"FILL ME IN"> expected but was  <true>.

Please meditate on the following code:
  /home/alex/local/ruby/koans/about_objects.rb:5:in `test_everything_is_an_object'

when you lose, don't lose the lesson
your path thus far [.X________________________________________________] 8/280
user:~/koans$

This really helps understand some of the nuances underpinning Ruby.

Monday, May 14, 2012

Ruby on Rails Resources for Beginners

I'll give my overview of "Ruby on Rails" then list off resources for beginners.

Caution: I'm a student, like anyone else, so do not take my notes as the Gospel.

Overview of Ruby on Rails

So the three languages of the internet are: Ruby, Python, and JavaScript. But if you know Ruby on Rails, you will always have a job.

Ruby on Rails is peculiar to get started with (well, for me anyways). What's the basic idea?

Most people end up introducing Rails with some jargon about "MVC" (Model-View-Controller), Don't Repeat Yourself, Convention over Configuration...and from those three slogans alone, you should be able to program Rails applications.

Unfortunately, I am not that smart. So these are some notes to myself as far as what some of these slogans mean!

Imagine a car. People are concerned about how it looks, what color it is, etc. (the "View"). Others are concerned with what's going on under the hood (the "model"), and others worry about how the driver interfaces with the car and the car responds (the "controller").

For us, the Model usually is the data structures used. This is some flavor of ActiveRecord (the API is available online [rubyonrails.org] for the "Base" class).

Great, now what's an "Active Record" and why should anyone give a damn? It's a way of describing the Object-relation mapping (ORM), i.e., encode a SQL database inside a Ruby object. For more about this, see Active Record [rubyonrails.org] for more details.

Lets be clear about this: the model is specified by active records. That's why we care. Got it? Good.

The view is much easier to understand: it's just a chunk of html. That's what the user views.

The controller is more-or-less taken care of, all thanks to the rails library. This is my understanding, at least. I may be completely wrong...

Resources

Several Screencasts [rubonrails.org] are available, and they are quite decent introductions!

You should first Try Ruby before doing rails; since rails is a ruby library...

The best walkthrough describing rails appears in a mere ten step program [tutsplus.com], and I must agree this is the best approach.

I found this YouTube Video (showing how to make a blog under ten minutes) quite enlightening, perhaps you might like it as well?