Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Ruby is the interpreted high-level programming language that supports various programming paradigms. It was designed with an emphasis on programming productivity and simplicity.
Ruby is an object-oriented language in the purest meaning possible because each value in the language behaves like (or is an object). Each object represents a particular class. An object responds to a set of methods defined by a class. Classes can extend, subclass, inherit, and override the methods of their superclass. Classes may also contain modules or inherit their methods from them.
A Simple Class
Classes and objects are crucial in object-oriented programming.
A class serves as a template from which objects can be built. The object is also referred to as a class instance. For example, the class "animal" includes mammals, birds, fish, reptiles, and amphibians as instances of the class.
Classes and objects can be simply created in Ruby. Write the class keyword, followed by the class name. The class name's initial letter needs to be capitalized.
Syntax
class Classname
end
The self keyword refers to the class that is being defined inside a class's body, but outside of any instance methods, it has specified.
Class is an expression in Ruby, like most other statements. The final expression in the class body determines a class expression's value. Usually, a def statement defining a method appears as the final expression within a class. A def statement's value is always nil.
Accessors & Attributes
Let's learn about Accessors and Attributes of Class in Ruby.
In a strict sense, attributes are instance variables of a class instance. In a broader sense, attributes are typically defined using attr_X type methods, whereas methods are merely declared as is.
Let’s define a class Point that uses two instance variables. Only other instance methods have access to the value of these variables. We must offer accessor methods that return the values of the variables if we want users of the Point class to be able to use the X and Y coordinates of a point:
class Point
def initialize(x,y)
@x, @y = x, y
end
def x # The accessor (or getter) method for @x
@x
end
def y # The accessor method for @y
@y
end
end
Now after defining these methods, we can write the code like this:
p = Point.new(1,2)
q = Point.new(p.x*2, p.y*3)
p.x and p.y are method invocations without parentheses.
Using Setters inside a Class
In order to change the values of the instance variables, we would also need to add setter methods to our Point class, making it mutable:
class MutablePoint
def initialize(x,y); @x, @y = x, y; end
def x; @x; end # The getter method for @x
def y; @y; end # The getter method for @y
def x=(value) # The setter method for @x
@x = value
end
def y=(value) # The setter method for @y
@y = value
end
end
attr_reader and attr_accessor Methods
Due to the prevalence of this instance variable setup using simple getter and setter methods, Ruby offers a mechanism to automate it. The Module class defines the attr accessor and attr reader methods. You can invoke these methods inside any class specification because all classes are modules.
Each technique accepts an unlimited number of symbol naming attributes. For instance variables with the same name, attr_reader generates simple getter methods. Getter and setter methods are created by attr_accessor.
For defining a mutable Point class:
class Point
attr_accessor :x, :y # Define accessor methods for our instance variables
end
For defining an immutable class:
class Point
attr_reader :x, :y # Define reader methods for our instance variables
end
Each of these techniques supports the acceptance of one or more attribute names as strings rather than symbols. Although using symbols is the preferred method, we may alternatively write code in the following way:
attr_reader "x", "y"
Instance methods are created for us by the attr, attr_reader, and attr_accessor methods.
This is an illustration of metaprogramming, and Ruby's capability to accomplish it is a strong feature. A class declaration, but not a method definition, is where attr and its related methods are called. They are only ever used once, during the class's definition. There are no performance issues in this case because the getter and setter methods they develop are just as quick as hand-coded ones. It's important to keep in mind that these methods can only produce simple getters and setters that correspond to the value of an instance variable of the same name.
You will need to create any more complex accessors, such as setters that set a variable with a different name or getters that return a value calculated from two different variables.
Frequently Asked Questions
How do you assign a string in Ruby?
In Ruby, strings can be created by enclosing a sequence of characters in either single quotes' or double quotations ". You have the option of using single or double quotations.
Can you iterate over a string in Ruby?
To transform a string into an array of characters, use the chars method. Then you can iterate over this array by using each.
What is a range in Ruby?
A range is a type of object that has a starting and ending value and allows you to design sequences that span the entire range between these two values.
What is a nested hash?
We can use nested hashes to group or associate the data we're working with even more. They assist us in dealing with scenarios in which a category or piece of data is linked to a collection of values rather than a single discrete value.
What are a truthy value and falsy value?
In a boolean context, such as an if statement, it's a value considered True and false, respectively.
Conclusion
In this article, we have extensively discussed Accessors and Attributes of Class in Ruby.
We hope this blog has helped you enhance your knowledge regarding Accessors and Attributes of Class in Ruby.