Introduction

About this book

This book was inspired by others like Learn You a Haskell and Why's Poignant Guide to Ruby, the latter being one of the books that originally taught me Ruby.

We wanted to capture some of that magic and show some of why we love Ruby, and why you should too. It was also inspired by Lemurs, because Lemurs are awesome.

It's currently a work in progress, and will be under heavy development during November for NaNoWriMo. Doesn't mean I won't still be writing afterwards, but we're going for a nice sized push this month.

More than likely you'll see this book move over to another publishing platform later when we start focusing on typesetting and page formats as Lemurs just don't like to hang around inline only.

As it's WIP, there may be some typos. Pull requests are welcome.

We hope you enjoy!

Is this book for me?

Do you like cartoons? Do you want to program? Great! This book is for you.

Don't know a single lick of programming, hate math, or otherwise find everything out there inaccessible? This book is still for you.

Already know a language or two and want to pick up on Ruby? You might find this book a bit basic, but you'll nonetheless enjoy the rampant puns and illustrations. I would recommend Eloquent Ruby as a quick introduction if you want something a bit faster though.

The Authors

Brandon Weaver

I like Ruby, I like art, and I like Lemurs. That's me, I draw and you know things. I've been working professionally with Ruby for over 6 years.

You can find me on IRC and Github as @baweaver and on Twitter as @keystonelemur.

Havenwood

Havenwood needs to write a bio, but he's off at RubyConf for now. We also need to get him a snazzy picture too.

Why Ruby?

Ruby was designed to make people happy. Ruby makes me happy, and it can make you happy too!

It was created by a particularly happy fellow by the name of Yukihiro Matsumoto, who we all call Matz. He's such a nice guy that the community has adopted a rule from it called MINASWAN: Matz is nice and so we are nice.

The entire language was made to resemble a conversation one might have with their computer, just take a look at this:

8.times { print "Na" }
print " LemurMan!"

Try reading that out loud. Sounds like you just told the computer what-for, good on you! Copywrites and caped crusaders aside, you've just taken a look at your first Ruby program. That's all there is to it.

Ruby is a very simple language to pick up, but don't let that deceive you. It's also an incredibly powerful and expressive language used by industry professionals for some pretty interesting apps.

Want to manage a few thousand computers? Ruby can do that, Chef can cook up a pretty mean server.

Got a web site to build? Hop on the train, we're going on Rails.

Have a bit more of a nefarious streak on you? Metasploit may be up your alley.

Thing is, Ruby can do a lot of things, and with a little imagination and a lot of coffee it can do what you want it to do too!

Just be careful not to call it Java. Some get touchy about that word.

The Cast

As the title indicates, this is an illustrated guide. That means that we get pictures, bad jokes, and puns. Sometimes you get all three, no extra charge.

For it to be an illustrated guide we're going to need a few characters for our cast. Shall we meet a few of them?

Aside Lemurs

These Lemurs are used as side notes or warnings in the book.

Dastardly Lemur

Careful! When you see this character it's best to be on guard because there's something that may trip you up. He's out to trip up new programmers however he can, and he may well be after you next!

That's ok though, if we keep an eye out we can avoid his little traps and make some awesome Ruby.

Scholarly Lemur

When you see the Scholarly Lemur, she has some more details on what was just covered that you may find interesting. That said, you don't have to read her sections to learn the basics.

Wizard Lemur

Wizard Lemur brings with him a series of black and dark magics, arcane arts best left alone by those wise in the ways of Ruby.

While one can do the things he prescribes and while they are really cool to see it might be best to go easy on the magic.

Just keep him out of the set_trace_func code or we'll all blow up.

Good Magic Lemur

While there's certainly dark magics in the world of Ruby, there are also some really great things you can do with them if you wield them properly.

Good Magic Lemur is here to show you how magic can be used effectively.

Startup

Installing Ruby

Lucky for us, the official Ruby site has instructions on how to install Ruby for many common operating systems: https://www.ruby-lang.org/en/downloads/

Yep, even Windows.

We won't worry too much about things like version management or any of that stuff that Developers use, for now we just need a working version of Ruby.

All examples in this book are run on Ruby 2.4.2, though Ruby 2.3.x will work.

Our good friend Ryan Bigg wrote some tutorials on installing Ruby for Mac and Ubuntu that he keeps fairly up to date. For now you only need to follow along to right before the section labeled Rails.

Linux Guide: http://ryanbigg.com/2014/10/ubuntu-ruby-ruby-install-chruby-and-you

Mac Guide: http://ryanbigg.com/2015/06/mac-os-x-ruby-ruby-install-chruby-and-you

After you have that installed, it's time to drop into our Command Prompt / Terminal.

Command whatsit?

Never played with a command prompt? That's ok, what it is is a text-only interface for your computer that you can put commands into.

Different Operating Systems put it in different places, so go ahead and find yours:

Windows

Open your start menu and type cmd into the search bar.

Mac OSX

Press CMD + Space to open finder, and type in Terminal

Linux

Depending on your distro it could be in several places, though typically you'll find it as konsole, terminal, console, or another similar name.

They never could quite agree on those things, try asking some of them how to say GNU some time to see. Also be sure you know how to run fast after you do.

IRB (Interactive Ruby)

So now that you have that open, go ahead and type in `irb` and press enter. You should be greeted with a line showing your Ruby version like this:

2.4.2 :001 >

On the left you'll see a number like 2.4.2. That's your Ruby version.

Next to it you'll see another number like :001. That's the current line IRB is on.

> is just the start of our prompt to tell us where we can start putting awesome Ruby code in.

Now go ahead and type in 1 + 1 and press enter!

2.4.2 :001 > 1 + 1
 => 2

So what exactly is an IRB? It's what we call a REPL (Read Evaluate Print Loop). So what's that?

We read a command from the user: 1 + 1

We evaluate it: 1 + 1 -> 2

We print the result: => 2

…and we loop back for more input.

You can even try out program from earlier:

2.4.2 :002 > 8.times { print "Na" }
NaNaNaNaNaNaNaNa => 8
2.4.2 :003 > print " LemurMan!"
 LemurMan! => nil

Take a few seconds to play with it.

For starting out we'll be using IRB a lot to get comfortable with the basics. Later on if you stick with Ruby it will become an invaluable asset for playing with code, though many may go over to using another REPL like Pry.

Don't worry, we'll get into text editors and running programs later.

Some Commentary

In some of the code you'll notice a # or two. These are called comments and don't get run by Ruby. We leave these in here to explain what something might be doing a bit more clearly, or to show what something returns:

2.4.2 :004 > 1 + 1 # => 2
 => 2

Everything after the # will not be run, but if you do run that code you'll get back 2.

Now the comment could literally be anything. Ruby doesn't care, it just wants the code. Comments are for we peoplefolk:

2.4.2 :005 > 1 + 1 # Comments fight for the user!
 => 2

Getting Help

Hit a dead end? Need some advice? There are a few places to start looking.

Google

The source above all sources, sometimes a quick Google search of your error will yield a good number of answers to your questions. Most times this will get you a solid answer.

Stack Overflow

Stack Overflow is a massive Programming question and answers site, and chances are if you've used Google to find an error message you ended up on here.

Make sure to read up on how to ask a good question: https://stackoverflow.com/help/how-to-ask

IRC

Internet Relay Chat is another valuable tool in asking questions. #ruby and ##new2ruby on Freenode are good places to ask questions.

Never used IRC? Just head over to http://webchat.freenode.net/ and join the #ruby channel and you're good to go for starting out.

You can find us (baweaver and havenwood) online on most all Ruby related channels.

results matching ""

    No results matching ""