Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Ruby
3.
Lexical Structure in Ruby
4.
Identifiers in Ruby
4.1.
Case Sensitivity in Ruby 
4.2.
Identifiers using Unicode
4.3.
Punctuations in Identifiers
5.
Frequently Asked Questions
5.1.
What is Ruby?
5.2.
Ruby is said to be a combination of which five languages?
5.3.
What are identifiers in ruby?
5.4.
Is Ruby Case Sensitive?
5.5.
What is the difference between single @ sign and double @ sign as prefixes in Ruby?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Identifiers in Ruby

Author Gaurav Joshi
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Every human being has a name and identifies itself with the same. Similarly, identifiers in ruby are nothing but just simply a name. Name to a variable, method, classes, etc., are all identifiers in ruby. The way the name of a human being helps us to identify the person, the identifiers in ruby help us identify the variable, class, method, etc. 

We will be looking into details about identifiers in ruby in the below section, but before that, let us have a look and understand ruby.

Ruby

 

Ruby

Ruby

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages  (Perl, Smalltalk, Eiffel, Ada, and Lisp). Often, its creator  Yukihiro "Matz" Matsumoto considers ruby as a human being in the sense that the way human beings are simple from the outside but complex from the inside. Like human bodies, ruby is simple in appearance but complex on the inside. So I think you get to know in brief about ruby let us have a brief idea about lexical structure in ruby before we go into details to understand identifiers in ruby.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Lexical Structure in Ruby

Like Human Languages, Computer languages also follow a lexical structure. A source code of the ruby program consists of tokens. While parsing any program, the ruby-interpreter parses the program as a sequence of these tokens. Tokens in ruby include literal, punctuation, comments, identifiers, and keywords. This article introduces you to one type of token, i.e., Identifiers in ruby. Tokens also include essential information about the characters that comprise the tokens and the whitespace that separates the tokens.

Identifiers in Ruby

As we have already defined identifiers in the introduction section of the blog, An identifier in ruby is simply a name. Ruby uses these identifiers to name variables, classes, methods, etc. Identifiers in ruby consist of letters, numbers, and underscore characters, but they do not begin with a number. Also, Identifiers in ruby do not include whitespace or nonprinting characters, or punctuation characters except as described here. 

Identifiers in ruby that begin with a capital letter A–Z are constants. If the user tries to alter the value of such identifiers in ruby, The Ruby interpreter will give a warning (Please note this will be a warning but not an error). Class and module names must begin with initial capital letters. Let us look into some sample identifiers. 

  • i  
  • X2  
  • old_value  # multiword identifiers in ruby that are not constant
  • _internal    # Identifiers may begin with underscores
  • PI   # Constant
  • NEW_VALUE  # multiword identifiers in ruby for constant

Case Sensitivity in Ruby 

Ruby is a case-sensitive language, meaning lowercase identifiers in ruby are different from uppercase identifiers. E.g. Like_This is entirely different from LIKE_THIS. Similarly, Start is different from START.

Identifiers using Unicode

Rules for forming identifiers in ruby are defined in terms of ASCII characters that are not allowed. All characters lying outside the ASCII character set are valid in identifiers, including punctuation.

In the following Ruby code, a UTF-8 encoded file is valid: 

def ×(x,y) # Method name is a Unicode multiplication sign 
     x*y # Method body multiplies its arguments
end.

 

Identifiers in ruby may not begin with an ASCII digit, but possible to begin with a digit from a non-Latin alphabet. Similarly, for an identifier to be considered a constant must begin with an ASCII capital letter. The identifier Å, for example, is not a constant. This indicates that special rules about forming identifiers are based on ASCII characters and are not enforced for characters outside of that set. 

Two identifiers are the same only if the same sequence of bytes represents them

Punctuations in Identifiers

Identifiers in Ruby can have punctuation characters at the start or end and have different meanings. We have explained what it means briefly based on the occurrence of different punctuation characters.

Punctuation Characters

Examples

Meaning

$ $new_files  The dollar sign in identifiers in ruby is used as a prefix for a global variable. Like Perl's, Ruby defines several global variables that include other punctuation characters, such as $_ and $-K.
@ @@counter  @ sign is used as a prefix in an instance and class variables. Ruby's class variables are prefixed with two @ signs, while Instance variables are prefixed with a single @ sign.
? Void?  Method returning boolean values in ruby often have names with a question mark at the end. This is a helpful convention followed in Ruby.
! Sort!  Any method name ending with an exclamation point indicates the method needs to be used cautiously. Ruby follows this convention to distinguish mutator methods that alter the object on which they are invoked from variants that give a modified copy of the original object
= timein=  Identifiers whose names end with an equal sign are used for methods that We can invoke by placing the method name on the left side of an assignment operator without the equal signs. 

Frequently Asked Questions

What is Ruby?

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages.

Ruby is said to be a combination of which five languages?

Ruby, an open-source dynamic language, is a perfect blend of five different languages (Perl, Smalltalk, Eiffel, Ada, and Lisp).

What are identifiers in ruby?

Identifiers in ruby are nothing but just simply a name. Name to a variable, method, classes, etc., are all identifiers in ruby. The way the name of a human being helps us to identify the person, the identifiers in ruby help us identify the variable, class, method etc.

Is Ruby Case Sensitive?

Ruby is a case-sensitive language, which means lowercase identifiers in ruby are different from uppercase identifiers. E.g. Like_This is entirely different from LIKE_THIS. Similarly, Start is different from START.

What is the difference between single @ sign and double @ sign as prefixes in Ruby?

We use @ sign as a prefix for an instance and class variables. Ruby's class variables are prefixed with two @ signs, while Instance variables are prefixed with a single @ sign.

Conclusion

In this article, we have studied  Identifiers in Ruby in detail and also briefly explore case sensitivity, Punctuation in identifiers in ruby and identifiers using Unicode covered all about it.

Also, visit our Guided Path in  Coding Ninjas Studio to learn about  Ruby. Upskill yourself in Ruby on RailsBackend Web TechnologiesHadoopSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design and much more!

To learn more, refer to Operational DatabasesNon- relational databasesMongoDBTop-100-SQL-problemsinterview-experienceIntroduction to ruby on railsDirectory Structure in RubyRuby on RailsRuby vs Python. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQRuby KoansRuby DocWhy Ruby?

We expect that this article must have helped you enhance your knowledge on the topic of Ruby. Please upvote our blogs if you find them engaging and helpful! 

We wish you all the best for your future adventures and Happy Coding. 

Conclusion

Previous article
Literals in Ruby
Next article
Keywords in Ruby
Live masterclass