Ruby Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is Ruby

A

Ruby is a pure object oriented programming language. It was created in 1993 by Yukihiro Matsumoto of Japan. Ruby is a general-purpose, interpreted programming language like PERL and Python.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a method?

A

Ruby methods are very similar to functions in any other programming language. Ruby methods are used to bundle one or more repeatable statements into a single unit.

Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.

Methods should be defined before calling them otherwise Ruby will raise an exception for undefined method invoking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Class Methods?

A

When a method is defined outside of the class definition, the method is marked as private by default. On the other hand, the methods defined in the class definition are marked as public by default. The default visibility and the private mark of the methods can be changed by public or private of the Module.

Whenever you want to access a method of a class, you first need to instantiate the class. Then, using the object, you can access any member of the class.

Ruby gives you a way to access a method without instantiating a class. Let us see how a class method is declared and accessed:

class Accounts
   def reading_charge
   end
   def Accounts.return_date
   end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an alias statement?

A

This gives alias to methods or global variables. Aliases can not be defined within the method body. The aliase of the method keep the current definition of the method, even when methods are overridden.

Making aliases for the numbered global variables ($1, $2,…) is prohibited. Overriding the builtin global variables may cause serious problems.

Syntax:
alias method-name method-name
alias global-variable-name global-variable-name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is ruby a pure OO language?

A

Ruby is pure object-oriented language and everything appears to Ruby, as an object. Every value in Ruby is an object, even the most primitive things: strings, numbers and even true and false. Even a class itself is an object that is an instance of the Class class. This chapter will take you through all the major functionalities related to Object Oriented Ruby.

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and methods within a class are called members of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Class?

A

When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name and is delimited with an end. For example we defined the Box class using the keyword class as follows:

class Box
code
end
The name must begin with a capital letter and by convention names that contain more than one word are run together with each word capitalized and no separating characters (CamelCase).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define a ruby object?

A

A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class using new keyword. Following statements declare two objects of class Box:

box1 = Box.new
box2 = Box.new
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the initialize method?

A

The initialize method is a standard Ruby class method and works almost same way as constructor works in other object oriented programming languages. The initialize method is useful when you want to initialize some class variables at the time of object creation. This method may take a list of parameters and like any other ruby method it would be preceded by def keyword as shown below:

class Box
   def initialize(w,h)
      @width, @height = w, h
   end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an instance variable?

A

The instance variables are kind of class attributes and they become properties of objects once we objects are created using the class. Every object’s attributes are assigned individually and share no value with other objects. They are accessed using the @ operator within the class but to access them outside of the class we use public methods which are called accessor methods. If we take above defined class Box then @width and @height are instance variables for the class Box.

class Box
   def initialize(w,h)
      # assign instance avriables
      @width, @height = w, h
   end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the accessor method?

A

!/usr/bin/ruby -w

To make the variables available from outside the class, they must be defined within accessor methods, these accessor methods are also known as a getter methods. Following example shows the usage of accessor methods:

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end

# accessor methods
def printWidth
@width
end

def printHeight
@height
end
end

# create an object
box = Box.new(10, 20)
# use accessor methods
x = box.printWidth()
y = box.printHeight()

puts “Width of the box is : #{x}”
puts “Height of the box is : #{y}”

produces:

Width of the box is : 10
Height of the box is : 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the setter method?

A

!/usr/bin/ruby -w

Similar to accessor methods which are used to access the value of the variables, Ruby provides a way to set the values of those variables from outside of the class using setter methods, which are defined as below:

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end
   # accessor methods
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   # setter methods
   def setWidth=(value)
      @width = value
   end
   def setHeight=(value)
      @height = value
   end
end
# create an object
box = Box.new(10, 20)

use setter methods

box. setWidth = 30
box. setHeight = 50

# use accessor methods
x = box.getWidth()
y = box.getHeight()

puts “Width of the box is : #{x}”
puts “Height of the box is : #{y}”

produces:

Width of the box is : 30
Height of the box is : 50

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are instance methods?

A

!/usr/bin/ruby -w

The instance methods are also defined in the same way as we define any other method using def keyword and they can be used using a class instance only as shown below. Their functionality is not limited to access the instance variables, but also they can do a lot more as per your requirement.

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end
   # instance method
   def getArea
      @width * @height
   end
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"

produces:

Area of the box is : 200

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are class variables?

A

he class variables is a variable which is shared between all instances of a class. In other words, there is one instance of the variable and it is accessed by object instances. Class variables are prefixed with two @ characters (@@). A class variable must be initialized within the class definition as shown below.

A class method is defined using def self.methodname() which ends with end delimiter and would be called using class name as classname.methodname as shown in the following example:

class Box
   # Initialize our class variables
   @@count = 0
   def initialize(w,h)
      # assign instance avriables
      @width, @height = w, h
  @@count += 1    end

def self.printCount()
puts “Box count is : #@@count”
end
end

# create two object
box1 = Box.new(10, 20)
box2 = Box.new(30, 100)
# call class method to print box count
Box.printCount()

produces:

Box count is : 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Access control: Public Methods

A

!/usr/bin/ruby -w

Public methods can be called by anyone. Methods are public by default except for initialize, which is always private.

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end

# instance method by default it is public
def getArea
getWidth() * getHeight
end

   # define private accessor methods
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   # make them private
   private :getWidth, :getHeight
   # instance method to print area
   def printArea
      @area = getWidth() * getHeight
      puts "Big box area is : #@area"
   end
   # make it protected
   protected :printArea
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
# try to call protected or methods
box.printArea()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Access control: Private Methods

A

!/usr/bin/ruby -w

Private methods cannot be accessed, or even viewed from outside the class. Only the class methods can access private members.

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end

# instance method by default it is public
def getArea
getWidth() * getHeight
end

   # define private accessor methods
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   # make them private
   private :getWidth, :getHeight
   # instance method to print area
   def printArea
      @area = getWidth() * getHeight
      puts "Big box area is : #@area"
   end
   # make it protected
   protected :printArea
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
# try to call protected or methods
box.printArea()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Access control: Protected methods

A

!/usr/bin/ruby -w

A protected method can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end

# instance method by default it is public
def getArea
getWidth() * getHeight
end

   # define private accessor methods
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   # make them private
   private :getWidth, :getHeight
   # instance method to print area
   def printArea
      @area = getWidth() * getHeight
      puts "Big box area is : #@area"
   end
   # make it protected
   protected :printArea
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
# try to call protected or methods
box.printArea()
17
Q

What is Class inheritance?

A

!/usr/bin/ruby -w

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.

Inheritance also provides an opportunity to reuse the code functionality and fast implementation time but unfortunately Ruby does not support Multiple level of inheritances but Ruby supports mixins. A mixin is like a specialized implementation of multiple inheritance in which only the interface portion is inherited.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class or superclass, and the new class is referred to as the derived class or sub-class.

Ruby also supports the concept of subclassing ie. inheritance and following example explains the concept. The syntax for extending a class is simple. Just add a < character and the name of the superclass to your class statement. For example, following define a class BigBox as a subclass of Box:

# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end
   # instance method
   def getArea
      @width * @height
   end
end
# define a subclass
class BigBox < Box
   # add a new instance method
   def printArea
      @area = @width * @height
      puts "Big box area is : #@area"
   end
end
# create an object
box = BigBox.new(10, 20)
# print the area
box.printArea()

produces:

Big box area is : 200

18
Q

What is a class constant

A

!/usr/bin/ruby -w

You can define a constant inside a class by assigning a direct numeric or string value to a variable which is defined without using either @ or @@. By convention we keep constant names in upper case.

Once a constant is defined, you can not change its value but you can access a constant directly inside a class much like a variable but if you want to access a constant outside of the class then you would have to use classname::constant as shown in the below example.

# define a class
class Box
   BOX_COMPANY = "TATA Inc"
   BOXWEIGHT = 10
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end
   # instance method
   def getArea
      @width * @height
   end
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
puts Box::BOX_COMPANY
puts "Box weight is: #{Box::BOXWEIGHT}"

produces:

Area of the box is : 200
TATA Inc
Box weight is: 10

19
Q

what is a block?

A

You have seen how Ruby defines methods where you can put number of statements and then you call that method. Similarly Ruby has a concept of Bolck.

A block consists of chunks of code.

You assign a name to a block.

The code in the block is always enclosed within braces ({}).

A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block.

You invoke a block by using the yield statement.