Skip to Content
Ruby on Rails For Dummies®
book

Ruby on Rails For Dummies®

by Barry Burd
January 2007
Beginner
330 pages
6h 52m
English
Wiley
Content preview from Ruby on Rails For Dummies®

Chapter 15. Ten Features That Set Ruby Apart

In This Chapter

  • Exploring Ruby's oddities

  • Using mixins, unit testing, and other stuff

  • Quacking like a duck

This chapter highlights some of Ruby's unique features. I list the features in no particular order. I cover many of these features in Chapters 5 and 6, but a few of this chapter's features blaze new Ruby territory.

Hashes

A hash is a collection of key/value pairs. Use curly braces {} to define a hash; use brackets [] to refer to a particular hash value.

price_of = 
  {'Book' => 20.00, 'Shirt' => 15.05, 'Cup' => 10.20}
price_of['Car'] = 23999.99

puts price_of['Car']

The output of this brief program is 23999.99.

For more info, see Chapter 5.

Open Classes

You can add definitions to a class at any point in the code. For example, in the following program, the definition of MyClass ends before the creation of my_object. Then the MyClass definition resumes after the creation of my_object.

class MyClass
  def my_method
    10
  end
end

my_object = MyClass.new
print my_object.my_method, " "

class MyClass
  def another_method
    22
  end
end

print my_object.another_method

The output of this program is 10 22.

For more on open classes, see Chapter 6.

Duck Typing

If it walks like a duck and it quacks like a duck, then it's a duck. Consider the following program. At first, the value variable walks and quacks like an integer. At that point in the code, value is an integer. So value.times is legal but value.each is illegal. (If you uncomment the first value.each line and try to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ruby on Rails: Up and Running

Ruby on Rails: Up and Running

Bruce Tate, Curt Hibbs
Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Learning Rails 5

Learning Rails 5

J. Mark Locklear, Eric J Gruber, Barnabas Bulpett

Publisher Resources

ISBN: 9780470081204Purchase book