background preloader

For review

Facebook Twitter

Instantiation and the Initialize Method. When you define a class in Ruby, Ruby will assign a new class object to the class name constant.

Instantiation and the Initialize Method

For example, if you were to say class Person; end, this is roughly equivalent to Person = Class.new. This class object is of the type Class, and holds a number of methods useful for making instances of copies of those instances. Making Instances To make a new instance of a class, call that class's new method. By default, this will allocate the required memory for the class and return a reference to the new object. While at first this seems a bit backwards, there is no new keyword in Ruby or any special syntax.

Initializing Instances A blank object is not very exciting. Class Person def initialize(name, age) @name, @age = name, age end end bob = Person.new('Bob', 34) You can also use this opportunity to acquire any resources you may need. Destorying Objects In general, you don't destroy objects in Ruby. If you're wondering about resources, don't worry about it. Ruby Best Practices- The Complete Class. A remark: we enabled comment moderation because the blog was recently target of spam.

Ruby Best Practices- The Complete Class

You probably have not seen much of it because we were pretty quick in removing it manually. So if your comment does not show up please be patient. There are some basic concepts (often called “aspects”) that need to be implemented for many classes although not all classes need all (or even any) of them: initialization conversion to a printable string equivalence hash code calculation comparability cloning (clone and dup) freezing customized persistence (Marshal and Yaml) matching math and operator overloading Which of these is needed for a particular class depends of course completely on the circumstances. We will look at these concepts individually in subsequent sections. Mutable fields redundant fields, i.e. fields which carry cached values that can be derived from other fields at least two fields for ordering priorities. Ruby Classes and Objects. Ruby is a perfect Object Oriented Programming Language.

Ruby Classes and Objects

The features of the object-oriented programming language include: Data Encapsulation: Data Abstraction:Polymorphism:Inheritance: These features have been discussed in Object Oriented Ruby. An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. Take the example of any vehicle. A vehicle can also have certain functions, such as halting, driving, and speeding. A class Vehicle can be defined as: Class Vehicle{ Number no_of_wheels Number horsepower Characters type_of_tank Number Capacity Function speeding { } Function driving { } Function halting { }} By assigning different values to these data members, you can form several instances of the class Vehicle.

Defining a Class in Ruby: To implement object-oriented programming by using Ruby, you need to first learn how to create objects and classes in Ruby. Ruby Hashes. A Hash is a collection of key-value pairs like this: "employee" => "salary".

Ruby Hashes

It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary and will generally not be in the insertion order. If you attempt to access a hash with a key that does not exist, the method will return nil. Creating Hashes: As with arrays, there is a variety of ways to create hashes. Ruby Arrays. Ruby arrays are ordered, integer-indexed collections of any object.

Ruby Arrays

Each element in an array is associated with and referred to by an index. Array indexing starts at 0, as in C or Java. A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects. Ruby arrays are not as rigid as arrays in other languages. Creating Arrays: There are many ways to create or initialize an array.

Names = Array.new You can set the size of an array at the time of creating array: names = Array.new(20) The array names now has a size or length of 20 elements. . #! This will produce the following result: You can assign a value to each element in the array as follows: #! Macmacmacmac You can also use a block with new, populating each element with what the block evaluates to: #! Nums = Array.[](1, 2, 3, 4,5) Ruby Classes and Objects.