recitation3 notes


Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e characters. c. Identifier names may consist of alphanumeric characters and the underscore character ( _ ). d. You can distinguish a variable’s type by the initial character of its identifier 5. Reserved words a. Ruby has a set of reserved words that may not be used as constant or local variable names. b. List of reserved words Topic 4: Ruby is Object­Oriented Ruby has classes. Classes hold data—in the form of variables and constants—and methods, which are compact collections of code that help you perform operations on data. Classes can inherit information from each other, but only one at a time. This allows you to reuse code—which means you'll spend less time fixing or debugging code—and intermix the code through inheritance. 3 In Ruby, almost everything is an object. In fact, everything that Ruby can bind to a variable name is an object. Let’s do an example. class Hello def howdy greeting= “Hello, Matz!” puts greeting end end class Goodbye < Hello def solong greeting = “Goodbye, Matz!” puts greeting end end friendly = friendly.howdy friendly.solong If we run the code above, we will get back: Hello, Matz! Goodbye, Matz. The reason is: The Hello class defines the howdy method. The Goodbye class likewise contains the definition of a method, solong. The Goodbye class also inherits what's in the Hello class (that's what the < symbol is for). This means that the Goodbye class didn't have to redefine the howdy method. It just inherited it. friendly is an object (an instance of the Goodbye class). You can use the friendly object to call both the howdy and solong methods, because it inherently knows about them. It knows about the solong method because it is defined inside the Goodbye class, and it knows about the howdy method because Goodbye inherited it from Hello. 4 Topic 5: Declaring Variables 1. Ruby doesn’t have type declarations. It just assigns values to variables a. Example: months = 12 2. Local Variables: a. Must start with a lowercase letter or with an underscore character b. Example 1: alpha c. Example 2: _beta d. When a local variable is defined inside a method or a loop, its scope is within the method or loop where it was defined. 3. Instance Varia...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online