Home > Articles > Programming > Ruby

The Meta-Guide To Learning Ruby

Really mastering a new programming language, certainly one as flexible and fluid as Ruby, is not easy. Russ Olsen offers some tips to ease the pain and speed yourself down the road to Ruby mastery.
Like this article? We recommend

Like this article? We recommend

I've been teaching Ruby off and on for a number of years. Actually that's not really true. It would be more accurate to say that I've coached people into teaching themselves Ruby. I don't think that anyone has ever really been taught a programming language: Learning a new programming language is one of those things that you need to do for yourself. Sure, you might have a cheerleader like me in the room while it is happening, someone who can help out a bit by saying "Look at this" or "Try that," but in the end you need to do the looking and you need to do the trying.

The key word here is trying. Really mastering a new programming language, certainly one as flexible and fluid as Ruby, is not easy. Ironically, it's the people who have really mastered some other language that seem to have the most trouble. It's a long fall from knowing exactly what you are doing, from building multi-threaded servers to stumbling on HelloWorld. Still there are things that you can do to ease the pain and speed yourself down the road to Ruby mastery.

Start by Focusing on the Familiar

The first thing is to build on what you already know. Fortunately, this is easy when you are learning Ruby. For example, I usually start teaching Ruby by showing some code and then asking a few questions. Here's the code:

class Employee
  attr_accessor :first_name, :last_name
  attr_reader :active
  def initialize( first_name, last_name )
    @first_name = first_name
    @last_name = last_name
  end
  def hire
    @active = true
  end
  def fire
    @active = false
  end
end

The questions are simple: What is that? There's usually some mumbling at first but it quickly resolves itself into a very clear answer: that's a class definition. How many methods? This time the answer is quicker: three. Which one do you think is the constructor? No hesitation now: The initialize method. And off we go.

I can get away with this because on one level Ruby is a very mundane object-oriented language. Anyone used to the ideas of OO programming will have no trouble deciphering my little example or any other simple Ruby class. The mundaneness of Ruby runs pretty deep: Ruby classes have instances, and an instance has exactly one class – no multiple inheritance allowed. When it comes to places to put data, Ruby has the familiar menagerie: there are local variables, method arguments, instance variables, and class variables. You call methods with the familiar object.method syntax. The best way to get started with Ruby is to focus on this familiar common ground. Look for the ways in which Ruby is exactly what you would expect and grind those bits into your psyche. There is no quicker way to learn something than to already know it in the first place.

Now Ruby is not exactly the same as your previous language. There are endless Ruby-esque flourishes on all of the familiar ideas: Ruby's single inheritance model allows for module inclusion, which is sort of like multiple inheritance. Ruby gives you a couple of different ways to hang data on a class. Ruby method calls don't generally require parentheses, so that along with the familiar puts( 'hello world' ) you can also say puts 'hello world'. The trick is to avoid getting stuck on the differences and focus on the things that are the same, at least in the beginning. You can build up a fair size kernel of Ruby skills by simply saying, “Oh yes, that's a class, I know what a class does.” Wait until you have built up a base of competency before you branch out into the strange stuff.

Don't Sweat the Small Stuff

And strange stuff you will find in profusion in Ruby. Some of the things that new Rubyists find strange about the language are deep and important; other things are shallow and trivial. We'll talk about the important stuff in a bit, but for the moment let's spend some time on the strange but trivial. Take the way that Ruby programmers indent their code: In Ruby, the rule is that you use two spaces for each level of indentation. It's always two spaces per level, never four, never eight or any other number. And it's always spaces, not tabs. This bugs a lot of new Ruby programmers. Programmers coming to Ruby from Java also tend to get irritated by the Ruby convention of using snake_case instead of camelCase names for variable names. And a surprising number of people are put off by the fact that the middle keyword in a multi-branch if statement is elsif, without the 'e'.

I can sympathize with all of this angst – in my early days I felt the same way. My suggestion for dealing with these little irritations is simple: Let yourself vent for a few minutes:

    Spaces! Really?

    But camelCase is so much better looking!

    Are they charging an extra $10 per vowel these days?

Let yourself go like this for a while and then move on. When you are learning a new programming language it is the trivia that tends to hit you in the face, but in the end it is just trivia. You weren't born with a tab key at the end of your finger. You learned to love camel case. No one has ever been confused about exactly what elsif means. Every language presents these kind of niggling irritations to newcomers, and the wise newcomer copes by simply working through the pain. You aren't learning Ruby because you love two space indentation or variable names with underscores, so don't reject it because of them. By the time you really understand the language you will either love it or you will hate it, but chances are that the spelling of elsif will not play into your decision.

Do Sweat the Big Stuff

Okay, so you've learned the parts of Ruby that seem familiar and you've managed to keep from putting your foot through the LCD over that missing 'e'. Now what? Now is the time to dig into the weird and important bits of Ruby.

Despite appearances, Ruby is not a mundane programming language. Ruby allows you to create instance variables, methods and classes on the fly: You can literally reprogram your application as you go. Ruby code can respond to calls to methods that have never been – and will never be – written. On top of this, every Ruby object has its own private class that lets it do things that no other object can do. As I mentioned, Ruby's modules give you a close approximation of multiple inheritance. Module inclusion also lets you rewrite the inheritance tree of any object, as your program is running. Oh, and Ruby is also a dynamically typed programming language, which means that it's the objects themselves and not the variables that refer to them that know about type.

All of this is why you don't want to waste your time on the syntactical trivia. The semantic power of Ruby is where the real action is and where you want to spend your energy. Try to keep an open mind as you work through all of these odd Ruby ideas. Remember, a very large number of programmers think that Ruby is a wonderful, elegant, fun-to-use tool. Many of those programmers – including me – came to Ruby after years of experience with more traditional programming languages. It is certainly possible that in the end you may decide that we are all mistaken, that we are all wrong. But we are not all nuts. The decisions that are built into Ruby go to the heart of how a programming language should work and what it should be. If you are going to take the time to learn Ruby, suspend your disbelief long enough to see it through the eyes of those who really like it. Then decide what you think.

There Is No King's Road to Ruby

As I said, the only person who is going to teach you Ruby is yourself. Sure, take a class (I know a guy...), read some books (I have a couple of suggestions...), and Google your way around the Internet. But do remember that mastering a new language means writing code, lots of code. That's why they call them programming languages. As you write each new program, build on the familiar aspects of Ruby and try to ignore the irritating parts. Most of all, challenge yourself to see what it is about this funny little language that makes so many of us so passionate about it.

Russ Olsen is the author of the upcoming book Eloquent Ruby, which, among other things, explains the solid engineering reasons behind Ruby's two space indentation rule, why dynamic typing is your best friend and how you might use the other crazy-sounding Ruby features. Russ's first book is the highly regarded Design Patterns in Ruby. Russ, who is now celebrating his second decade of Ruby programming, lives outside of Washington DC and at russolsen.com.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020