Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Variables in Ruby
3.
Assigning to Variables in Ruby: Types of Assignment
3.1.
Assignment
3.2.
Abbreviated Assignment
3.3.
Multiple Assignment
3.4.
Array Decomposition
4.
Frequently Asked Questions
4.1.
How is assigning to variables in Ruby done?
4.2.
What does @variable mean in Ruby?
4.3.
What does || mean in Ruby?
4.4.
What are the four types of variable scope in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024

Assigning to Variables in Ruby

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Ruby is a dynamic programming language with complex but expressive grammar and a core class library with a rich and powerful API. The variables used here are very easy to work with compared to other languages. Ruby draws inspiration from LispSmalltalk, and Perl, but uses a grammar that is easy for C and Java programmers to learn. Ruby is a pure object-oriented language, but it is also suitable for procedural and functional programming styles. It includes powerful metaprogramming capabilities and can be used to create domain-specific languages or DSLs.

Ruby by coding ninjas

Assigning to Variables in Ruby is one of the most crucial aspects of programming. In this article, we will explore different ways of assigning to variables in Ruby.

Variables in Ruby

Assigning to Variables in Ruby is done by the programmers for a variety of reasons, some of them are:

  • Variables in Ruby (e.g., patient address vs. '1234 Amityville Lane, Apt 3., Anytown, New York, 67890') are typically easier to remember and type than the actual information we're working with.
  • Assigning to variables in Ruby makes this process much easier to manage and follow for programmers who start with a piece of data in a particular state and conclude with it in a possibly-complicated alternative state.
  • Assigning to variables in Ruby makes code more manageable in the long run. Any programmer can look at your code and figure out what's going on with the correct variable names. This is crucial for long-term code management.
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

Assigning to Variables in Ruby: Types of Assignment

We have different types of ways by which assigning to variables in Ruby is done.

Assignment

In Ruby, the assignment uses the = (equals sign) character. This example assigns the number seven to the local variable a:

a=7


A local variable is created by the assignment if the variable was not referenced previously.

Abbreviated Assignment

We can mix several of the operators and assignments. To add 2 to an object we can write:

b = 1

b += 2


This is equivalent to:

b = 1

b = b + 2


We can use these operators this way: +, -, *, /, %, **, &, |, ^, <<, >>

There are also the operators ||= and &&=. If the value was nil or false, the former makes an assignment, while the latter makes an assignment if the value was not nil or false.

Consider the following example:

b ||= 0

b &&= 1


These two operators behave more similar to b || b = 0 than b = b || 0.

Multiple Assignment

On the right-hand side, we can assign numerous values to multiple variables in Ruby:

c, d = 1, 2

puts"c=#{c}, d=#{d}"


Output

c=1, d=2


We can use multiple assignments to swap two values:

old_value = 1

new_value=2

new_value, old_value = old_value, new_value

puts"new_value= #{new_value}, old_value= #{old_value}"


Output

new_value=1, old_value=2


If you have more values on the right-hand side of the assignment than variables on the left-hand side, the extra values are ignored:

a, b = 1, 2, 3

puts"a=#{a}, b=#{b}"


Output

a=1, b=2


We can use * to gather extra values on the right-hand side of the assignment.

a, *b = 1, 3, 4

puts"a=#{a}, b=#{b}"


Output

a=1, b=[3, 4]


The * can appear anywhere on the left-hand side:

*c, d = 3, 4, 5

puts"c=#{c}, d=#{d}"


Output

c= [3, 4], d=5


But we only use one * in an assignment.

Array Decomposition

You can dissect an Array during assignment using parenthesis, just like you do with method arguments:

(c, d) = [1, 2]

puts"c=#{c}, d=#{d}"


Output

c=1, d=2


As part of a broader multiple assignments, you can decompose an Array:

a, (b, c) = 1, [2, 3]

puts"a=#{a}, b=#{b}, c=#{c}"


Output

a=1, b=2, c=3


Because each decomposition is treated as a separate multiple assignment, you can use the * symbol to collect arguments in the decomposition:

a, (b, *c), *d = 1, [2, 3, 4], 5, 6

puts"a=#{a}, b=#{b}, c=#{c}, d=#{d}"


Output

a=1, b=2, c=[3, 4], d=[5, 6]

Frequently Asked Questions

How is assigning to variables in Ruby done?

Local variables in Ruby begin with the letter or a lowercase letter. A local variable's scope extends from the opening brace of a block to its closing brace, or from class, module, def, or do to the equivalent end.

What does @variable mean in Ruby?

In Ruby, the at-sign ( @ ) before a variable name (e.g. @variable_name ) is used to create a class instance variable.

What does || mean in Ruby?

The or-equals operator in Ruby lets you assign a value to a variable if and only if that variable evaluates to nil or false. ||= is the operator that accomplishes this. This operator has two pipes that indicate or and an equals sign that represents assigning a value.

What are the four types of variable scope in Ruby?

Ruby has four types of variable scope, local, global, instance and class. In addition, Ruby has one constant type. Each variable type is declared by using a special character at the start of the variable name as outlined in the following table.

Conclusion

In this article, we have extensively discussed the various ways of assigning to variables in Ruby. We began with a brief introduction to variables followed by an explanation and examples of assigning to variables in Ruby.

After reading about assigning values to variables in Ruby, are you not feeling excited to read/explore more articles on the Ruby programming language? Don't worry; Coding Ninjas has you covered.  To learn, see RubyRuby coding NinjasDocumentationOfficial Ruby FAQ, and Ruby Koans.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thank you image
Next article
Assigning to Constants in Ruby
Live masterclass