Let’s learn Ruby on Rails, want to?


Moving on…

Posted in ruby, rubyonrails by MrPeanut on the July 31, 2007

Okay, so now I feel like I’m grasping the basics of Ruby as a language. I’m far from understanding it completely or even being able to write my own Ruby programs, but I do feel like I’m getting the basics down. My education on the subject is not complete, but for the purpose of this blog, I think I want to move away from the Ruby aspect and start to focus on the Ruby on Rails part.

I’m moving on for two reasons. First, while I find Ruby fun to play with, the truth is that I’m not interested in becoming a programmer. I started this journey to learn Rails. That doesn’t mean that I’m going to stop learning Ruby altogether—actually, it’s quite the opposite. I’m still following Why’s (poignant) guide and I’m also using Chris Pine’s Learn to Program book to continue my education. The thing is, I want to learn Ruby to support my Rails development and make me better at Rails, not to make me a Ruby programmer. Second, it’s becoming increasingly difficult to express what I’m learning in Ruby on this blog. I’m not sure how to convey this information in a concise manner and still make it comprehensive enough to mean anything. My last post took quite a long time to format, only to show a snippet of code that’s only marginally useful.

Don’t worry, it’s not that I’m breaking up with Ruby… It’s just that we’ve decided to see other people.

Conditionals!

Posted in ruby, rubyonrails by MrPeanut on the July 25, 2007

Okay. So, yesterday I spent my time reading how to code conditional statements in Ruby. Because I do have a limited exposure to programming, this isn’t actually as bad as it sounds (or as it could be).

Basically, a common interaction for a program involves the computer stating that IF some condition is met, this happens—ELSE (the condition is not met) this other things happens. While I hate to take examples directly from Why’s (poignant) guide to Ruby (haven’t I already linked to this site enough?), the below code snippet does a great job of explaining several parts of a conditional statement way better than I ever could… besides, I’m the student, not the teacher, right?


print( if at_hotel.nil?
        “No clue if he’s in the hotel.“
             elsif at_hotel == true
        “Definitely in.“
             elsif at_hotel == false
        “He’s out.“
             else
        “The system is on the freee-itz.“
             end )

So, in this example, if at_hotel is empty (nil) then “No clue if he’s in the hotel.” will be printed to the screen. This means that the program doesn’t have enough info to answer the question. If at_hotel is TRUE, then it will print “Definitely in.” If at_hotel is FALSE, then “He’s out.” gets printed. Since this covers all the possibilities, the only other option is that system must be on the fritz.

Conditional statments aren’t so bad, right? In fact, everything has been pretty manageable up to this point. I have to admit that I’m getting worried, because in my past experience, this is around the time that the hammer usually drops. Usually at this point in the training, the next bit of code we see is so complicated and unrelated to anything we’ve learned, that it causes my eyes to cross and my brain to shut down. Hopefully that won’t be the case this time…

Two Parts

Posted in ruby by MrPeanut on the July 24, 2007

I read an interesting piece today in the poignant guide. It said that there are basically only two parts to Ruby: 1. Defining things and 2. Putting those things things in motion. I love this. Breaking everything about a programming language down into two simple parts makes me smile. It’s easy for me to get intimidated by lines and lines of code… but if it all boils down to two parts, how hard could it be?

Lesson 1, learning the basics of programming

Posted in ruby, rubyonrails by MrPeanut on the July 23, 2007

As I’ve mentioned before, I’m not a programmer by trade or by nature. I don’t really think analytically, so trying to learn a programming language is really hard for me. That means that this section is very important. Learning the basic concepts will help build a foundation for when things get to be more complicated. Yes, I know that the previous sentence is the most obvious statement in the history of time, but it is at this stage that I usually screw up. I don’t take enough time to learn the terminology or to really understand the basics and then shortly after, I’m lost.

I should mention that I’m using Why’s (Poignant) Guide to Ruby for the first part of this adventure. Why (as in why the lucky stiff) is, as best I can tell, a mad genius. His project Try Ruby! made me realize that I could do this—that I could learn a programming language even though I’ve failed so many times in the past. His latest project Hackety Hack, which is designed to make programming acessible to kids (in the way BASIC was to us back in the 80’s), made me truly understand his brilliance. It also made me a huge fan. I would urge you to check out all these sites, but particularly the last one for it’s unrivaled awesomeness.

Okay, on to what I’ve learned:

The first section of the guide is meant to warm us up to the terminology and the basic functions of Ruby code. I’m going to make a list here so I have a quick reference for the future. I think I’m going to abbreviate the notes I took, because I don’t want this post to be insanely long or detailed. I just want a quick guide. If none of this makes sense, I would strongly urge you to visit Why’s (Poignant) Guide to Ruby. Again, this is only meant to be a reference (and maybe a study guide). Also, there were a few sections that I wasn’t 100% clear on the information being conveyed. I will make note of that anywhere it’s applicable.

  • Variables → x, y, bananna2. Any plain, lowercase word in Ruby. Used as a nickname for something you use frequently. Syntax: teddy_bear_fee = 121.08
  • Numbers → 1, 23, -10000. Basic number are called “integers” while decimals are called “floats.” Use underscores to make large numbers readable. Syntax: 12_000_000_000
  • Strings → Any characters surrounded by quotes. Storing multiple characters with quotes causes them to be stored together as a single unit called a string. Single or Double quotes can be used. Syntax: avril_quote = “I’m a lot wiser. Now I know what the business is like.”
  • Symbols → :a, :b, :ponce_de_leon. Look like variables, but they begin with a colon. Used as lightweight strings, usually stored where you need a string but won’t be printing to screen. Update: Symbols can be used multiple times, but only get stored once, so they are easier on the computer.
  • Constants → Time, Array, Bunny_Lake_Is_Missing. Words like variables, but capitalized. They refer to something very specific and they don’t ever change. Syntax: EmpireStateBuilding = “350 5th Avenue, NYC, NY (because you can’t just move the Empire State Building!)
  • Methods → If variables and constants are nouns, methods are verbs. They are usually attached to the end of variables or constants by a dot. They can be chained together. Syntax: front_door.open.close
  • Method Arguments → Methods sometimes require more info to perform an action. The arguments are usually surrounded by parentheses and separated by commas. The following paints a door 3 coats of red paint. Syntax: front_door.paint( 3, :red )
  • Class Methods → Usually attached after variables and constants. Rather than a dot, a double colon is used. Syntax: Door::new( :o ak ) *need much more info about this one*
  • Global Variables → $x, $y, $chunky. Variable that begins with dollar sign. Unlike most variables, which are temporary, global variable will always be the same. They can be used anywhere in your program.
  • Instance Variables → @x, @y, @chunky_bacon. Variables that begin with the at symbol. Used to define attributes of something. For example, for front_door, we could set the width of the door with @width. Think of the at symbol as meaning attribute. *Need more info on this one*
  • Class Variables → @@x, @@y. Variables that begin with double at symbols. They are used at a higher level. Think of @@ is as attribute all. *Need more info on this one too*
  • Blocks → Any code surrounded by curly braces. Used for grouping a set of instructions so the code has become a single unit. Syntax: 2.times { print “something.” } Note: Curly braces can be exchanged for the words “do” and “end.” This works well when a block is longer than one line.
  • Block Arguments → Set of variables surrounded by a pipe and separated by commas. They are always used at the beginning of a block. Syntax: { |x,y| x+y } *Need more info*
  • Ranges → Two values surrounded by parentheses and separated by an ellipses (in the form of either two dots or three dots). Two dots returns the entire range, three dots causes the last value in the range to be excluded. Ranges can be numbers or letters. Syntax: (1..3) – provides a range from 1 through 3. (0…5) – provides a range from 1 through 4.
  • Arrays → A list surrounded by square brackets, separated by commas. It is a collection of things in a specific order. Syntax: [1, 2, 3] or ['coat', 'mittens', 'snowboard']
  • Hashes → Created by using the equals sign and the greater than sign. Matches a word with a definition. Hashes are easy to search through, and unlike arrays, items are not kept in a specific order. Syntax: { ‘a’ => ‘aardvark’, ‘b’ => ‘badger’}
  • Regular Expressions → Sometimes called “regexp” is a set of characters surrounde by slashes. They are used to find words or patterns in text. Syntax: /ruby/ or /[0-9]+/ or /^\d{3}-\d{4}/
  • Operators → Used to do math in Ruby, or to compare things.
    ** ! ~ * / % + - &
    << >> | ^ > >= < < <=>
    || != =~ !~ && += -= == ===
    .. not and or



  • Keywords → Built in words that have meaning in Ruby. They can not be used as variables and they can not be changed.
    alias
    and
    BEGIN
    begin
    break
    case
    class
    def
    defined
    do
    else
    elseif
    END
    end
    ensure
    false
    for
    if
    in
    module next
    nil
    not
    or
    redo
    rescue
    retry
    return
    self
    super
    then
    true
    undef
    unless
    until
    when
    while
    yield







Another point that Why makes is that Ruby language is much easier to read than traditional programming languages. In fact, in most cases, you should be able to read it aloud and have it make sense—which is very cool.

Okay, I think that’s enough for now. I hope this works as a handy little guide. I’m going to spend some time researching the few areas that I didn’t completely understand.

Why, Why, Why?

Posted in ruby, rubyonrails by MrPeanut on the July 21, 2007

So why do I want to learn Ruby on Rails anyway? The short answer is that I’m stuck. Once upon a time, I was a web developer who was ahead of the curve (or at least on the curve) with regard to current technology. I’m no programmer—far from it—but I was able to master html and css pretty early on and I did pretty well with other slightly more advanced technologies, such as ColdFusion. I loved my work because, for the most part, I had free reign to design concepts and build applications. Things were good.

Things are no longer good. I’m now in a situation where I don’t have any creative freedom or control. In fact, I’m not even developing anything… I’m just supporting a system that may never even fully launch. My day consists of mind-numbing tasks and multiple hour-long meetings. Not only has this crushed my creativity, but also my spirit. I’ve thought about switching jobs, but I’ve been in this holding pattern for so long, that my skills are out-of-date. And to be honest, I don’t really feel like doing anything anyway. I need a jumpstart.

I still enjoy following web trends, and I love what’s been happening over the past few years… as a user. As a developer (or, one-time developer) it scares me to death. What makes the web turn these days is much more complicated than simple html. I’ve tried learning object-oriented programming in the past… my brain just doesn’t work that way. Day 1, everything is crystal clear. Day 2, things are a bit fuzzy. Day 3, I’m lost and busy counting the tiles on the ceiling. I fear that in the new world, I’ve already been left behind.

Then I start to hear rumblings about Ruby on Rails. Until this point, I was sure it was just another language that was way too complicated for me to learn. But word on the street (er, the internet) is that it is a powerful and relatively simple and fairly intuitive web applications framework. This grabs my attention. If I were able to learn RoR, maybe I could break out of this situation I’m in… maybe I could develop some of these apps I’ve been thinking about for a while. Maybe.

What’s the point of all this?

Posted in ruby, rubyonrails by MrPeanut on the July 20, 2007

Well, it’s like this… I’m thinking I would like to learn Ruby and Ruby on Rails. I sometimes have trouble sticking with things, so I thought if I created a blog, I could use it as a learning aid. Posting what I’ve learned should help me reinforce the lessons I’ve worked on and should help me better understand the language. It should also work as a means to hold me accountable for studying. If I have to report to the blog, I had better have something to say.

I don’t much expect anyone to stumble upon this blog… however, if they do, hopefully they could follow my notes and maybe find something useful. Hey, you never know. If that someone happens to be you, I hope you find this site useful. Cheers.