Object, Class, and Methods in Ruby

BharteeTechRubyOnRails
3 min readMar 11, 2023

--

Object-oriented programming is a programming concept that uses objects and their instructions to design and program applications.

Example:

Student ≤=> class

- first name, last name, email, username, password => attributes

Individual students have their own details, consider my details in a database, and University works with my details, the system will create an object for me with this, by writing some function and working with my details.

In Ruby, everything is an object.

In Ruby, almost everything is an object.

All values ​​that can be handled in Ruby are objects.

The only operations that can be performed on objects in Ruby are method calls.

The methods an object can react to are uniquely determined by the class to which it belongs.

The class to which an object belongs is determined when the object is created, and the class to which it belongs will not change after that except for the introduction of a singleton class.

An object is also called an “instance” of a particular class, as opposed to it.

Objects that have classes:

Objects are concrete instances (manifestations) of classes.

That means that the following sentence is true: In Ruby, “a string” is a String.

we can say that an object is an instance of its class

"object have classes".class
=> String

"object have classes".is_a? String
=> true

Classes that create objects:

Objects are instances of classes.

Classes have a bunch of characteristics, but most importantly, every class defines a number of methods, which are specific to this type of thing (e.g. a String).

Objects inherit methods from their classes.

Every time a new object is created (“instantiated”)

Objects that have methods:

Methods add behavior that is useful to have for a particular type of object.

E.g. “hello”.upcase calls the method upcase on the String “hello”.

class and is_a? are methods defined on all objects in Ruby, and therefore also defined on the String “a string”: “a string”.is_a?(String) answers with true.

Some methods, such as class, is_a? are defined on all objects.

Calling methods:

In Ruby, methods that belong to (are defined on) objects can be used (called) by adding a dot, and then the method name, like so:

object.method

For example, the class String defines methods like upcase(what is the upcase version of yours), downcase (What’s the downcased version of yours), and length (Hey string, what’s your length?).

A dot is used to call a method on an object.

Most methods are questions and return a relevant value.

Some methods include commands to change the object, or the system (e.g. by saving a file).

name = "Calling Object"
=> "Calling Object"
name.length
=> 14
name.downcase
=> "calling object"
name.upcase
=> "CALLING OBJECT"

Passing arguments:

Sometimes an object needs a little bit of extra information in order to do what you ask for.

These extra bits of information are called arguments.

name = "Ruby Tutorial"
=> "Ruby Tutorial"
name.delete("by Tu")
=> "Rtorial"
name.prepend("Welcome to ")
=> "Welcome to Ruby Tutorial"

Chaining method calls:

When we call a method on an object it will return another object to us. We can then immediately call another method on that new object, and so on.

name = "Ruby Tutorial"
=> "Ruby Tutorial"
name.prepend("Welcome to ").upcase
=> "WELCOME TO RUBY TUTORIAL"

Predicate methods:

Predicate methods that end with a question mark ? return either true or false.

15.even?
=> false
15.odd?
=> true
10.between?(1,10)
=> true
20.between?(1,10)
=> false
name.start_with?("R")
=> true
name.end_with?("R")
=> false
name.include?("R")
=> true
name.include?("Y")
=> false
0.zero?
=> true
1.zero?
=> false
[1, 2].include?(1)
=> true
[1, 2].include?(3)
=> false
{ "1" => "one" }.key?("1")
=> true
{ "1" => "one" }.key?("2")
=> false

Bang Methods:

Bang methods end with an exclamation mark, and often modify the object they are called on.

# Example 1:  without using bang method.
name = "Ruby Tutorial"
=> "Ruby Tutorial"
puts name.downcase
ruby tutorial
=> nil
puts name
Ruby Tutorial
=> nil

# As you can see calling the method downcase! on the second line has modified the String itself (the object that name refers to), and also returned the new downcased version.
# Use bang methods with caution.
name = "Ruby Tutorial"
=> "Ruby Tutorial"

puts name.downcase!
ruby tutorial
=> nil

puts name
ruby tutorial
=> nil

--

--

BharteeTechRubyOnRails
BharteeTechRubyOnRails

Written by BharteeTechRubyOnRails

Ruby on Rails Developer || React Js || Rspec || Node Js

No responses yet