Ruby Is an Object-Oriented Language

Everything you manipulate in Ruby is an object, and the results of those manipulations are themselves objects.

When you write object-oriented code, you’re normally looking to model concepts from the real world. Typically, during this modeling process you discover categories of things that need to be represented. In an online store, the concept of a line item could be such a category. In Ruby, you’d define a class to represent each of these categories. You then use this class as a kind of factory that generates objects—instances of that class. An object is a combination of state (for example, the quantity and the product ID) and methods that use that state (perhaps a method to calculate the line item’s total cost). We’ll show how to create classes in Classes.

You create objects by calling a constructor, a special method associated with a class. The standard constructor is called new. Given a class called LineItem, you could create line item objects as follows:

 line_item_one = LineItem.​new
 line_item_one.​quantity​ = 1
 line_item_one.​sku​ = ​"AUTO_B_00"

You invoke methods by sending a message to an object. The message contains the method’s name, along with any parameters the method may need. When an object receives a message, it looks into its own class for a corresponding method. Let’s look at some method calls:

 "dave"​.​length
 line_item_one.​quantity​()
 cart.​add_line_item​(next_purchase)
 submit_tag ​"Add to Cart"

Parentheses are generally optional in method calls. In Rails applications, you’ll find that most method calls involved in larger expressions have parentheses, while those that look more like commands or declarations tend not to have them.

Methods have names, as do many other constructs in Ruby. Names in Ruby have special rules—rules that you may not have seen if you come to Ruby from another language.

Ruby Names

Local variables, method parameters, and method names should all start with a lowercase letter or with an underscore: order, line_item, and xr2000 are all valid. Instance variables begin with an at (@) sign—for example, @quantity and @product_id. The Ruby convention is to use underscores to separate words in a multiword method or variable name (so line_item is preferable to lineItem).

Class names, module names, and constants must start with an uppercase letter. By convention they use capitalization, rather than underscores, to distinguish the start of words within the name. Class names look like Object, PurchaseOrder, and LineItem.

Rails uses symbols to identify things. In particular, it uses them as keys when naming method parameters and looking things up in hashes. Here’s an example:

 redirect_to ​:action​ => ​"edit"​, ​:id​ => params[​:id​]

As you can see, a symbol looks like a variable name, but it’s prefixed with a colon. Examples of symbols include :action, :line_items, and :id. You can think of symbols as string literals magically made into constants. Alternatively, you can consider the colon to mean thing named, so :id is the thing named id.

Now that we’ve used a few methods, let’s move on to how they’re defined.

Methods

Let’s write a method that returns a cheery, personalized greeting. We’ll invoke that method a couple of times:

 def​ ​say_goodnight​(name)
  result = ​'Good night, '​ + name
 return​ result
 end
 
 # Time for bed...
 puts say_goodnight(​'Mary-Ellen'​) ​# => 'Goodnight, Mary-Ellen'
 puts say_goodnight(​'John-Boy'​) ​# => 'Goodnight, John-Boy'

Having defined the method, we call it twice. In both cases, we pass the result to the puts method, which outputs to the console its argument followed by a newline (moving on to the next line of output).

You don’t need a semicolon at the end of a statement as long as you put each statement on a separate line. Ruby comments start with a # character and run to the end of the line. Indentation isn’t significant (but two-character indentation is the de facto Ruby standard).

Ruby doesn’t use braces to delimit the bodies of compound statements and definitions (such as methods and classes). Instead, you simply finish the body with the end keyword. The return keyword is optional, and if it’s not present, the results of the last expression evaluated are returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.62.103