Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Ruby Methods
3.
Method Names
4.
Equal Sign Convention
5.
Question Mark Convention
6.
Exclamation Mark Convention
7.
Frequently Asked Questions
7.1.
What is Ruby?
7.2.
Is Ruby an Object Oriented Programming Language?
7.3.
What are Functions in Ruby?
7.4.
How many types of methods are present in Ruby?
7.5.
Does Ruby support data types?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Methods Names in Ruby

Author Gunjan Batra
0 upvote

Introduction

In this blog, we will understand the method names in ruby. Before going further let us know what is Ruby. Ruby is a highly object-oriented programming language, which is simple yet quite efficient. Ruby is used in desktop applications, static websites, and crawling. In this everything appears to be an object. There are different methods in Ruby. We will look at how these methods have a different role altogether in the program and its meaning. 

Ruby image

Before going further, let's first understand what a method is.

Ruby Methods

The method is simply a block of code or function that runs when it is called. Methods in Ruby are very similar to functions in any programming language. Unlike java, ruby methods have to be pre-defined before calling it. Otherwise, Ruby shows errors in compiling the program.

def method_name1
 exp.
end.


The methods can have parameters as well.

def method_name1(param = value, param2= value)
exp
end

Method Names

Usually, method names start with a lowercase letter. In every programming language,

Method names are kept in lowercase letters. The method name that starts with the uppercase letter is considered constant in Ruby. There might be a case where the method name is longer than usual. In this case, the name should be written with the help of underscore( _ )  and not in this manner. (worksFine). Ruby does not support the Camel case notation.

Method image

Now to declare a method, it is important to name them. For naming the methods, Ruby has different rules.

Although every method doesn't need to end with an equal sign, a question mark ❓ or an exclamation sign❗.

Table image

Now, let's see what these indications mean in Ruby.

Equal Sign Convention

=: The equals in the suffix of a method means that the method can be called by the assignment syntax also. This also shows that the method is a setter method, that sets some or the other value in the variable, object, or anything.

Example for setter method: 

class School 
  def initialize(name)
    @name = name
  end

  # Getter
  def name
    @name
  end
def name=(name)
    @name = name
  end

end
school = School.new("DPS SCHOOL")
puts(company.name)
school.name = "SJC SCHOOL"
puts(school.name)


Output

DPS SCHOOL
SJC SCHOOL


These setter methods help you to assign new values to your instance variables. If you don't have setter methods you are not allowed to change the value for your instance variable. Ruby shows an error in case you do so.

# => undefined method `name=' for #<School:0x00007fdd680f50f8 @name="DPS SCHOOL"> (NoMethodError)

Question Mark Convention

❓ The question mark in the method name does not have any specific task but by using the question mark in the method name, the programmer gets to know that the method will return a Boolean value. These are also called predicates. Predicates simply mean that the method will return a value that will be either true or false. For example: If an array has no elements and you find by using the array method empty, you will get the output true as no element is present in the array.

Exclamation Mark Convention

In the numeric case, it either returns nil if the number is not found or the number itself.

 ❗ The second convention is to use the exclamation mark. The method that has an exclamation mark indicates that it must be used with caution. To have a more clear understanding, let us consider the array method sort.

The sort method can be used in two ways.

At first, the array will create a copy and then sort the array.

Another case would be an array that will change the content of the original object.

In general, methods with exclamation are called mutators.

But not necessary every method changes the content of the original object.

Frequently Asked Questions

What is Ruby?

Ruby is a scripting language that is used in both front-end and back-end development. It is used in web development, web servers, web scraping, and crawling.

Is Ruby an Object Oriented Programming Language?

Ruby is a purely Object Oriented programming language. Everything for ruby acts as an object even the data types like strings, numbers, and even true or false also act as an object for Ruby.

What are Functions in Ruby?

Functions and methods are the same. The only difference they have is one belongs to an object. Functions are created by using the def keyword, and the code block is written inside it.

How many types of methods are present in Ruby?

Ruby classes have two main methods that are named classes and instances. These methods perform different tasks in the code.

Does Ruby support data types?

Ruby is an object-oriented programming language that supports all data types but in different categories. All the data types in Ruby are implemented as classes.

Conclusion

In this article, we have discussed the names of the methods in Ruby. We started with a brief introduction about the methods and how it is being used in the code. We even look at how we can name the ways in Ruby. We understood calling by code blocks and examples. In the end, we have seen the frequently asked questions about Ruby Programming language.

To get more information about Ruby, refer to the blogs mentioned below:

Boolean in Ruby

File Structure in Ruby

How to Invoke Ruby Interpreter

Symbols in Ruby

Arrays in Ruby

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Happy Learning, Ninjas!

Live masterclass