The Wayback Machine - https://web.archive.org/web/20240818171202/https://www.geeksforgeeks.org/operator-precedence-in-ruby/
Open In App

Operator Precedence in Ruby

Last Updated : 24 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Operators are used to perform different kinds of operations on operands. Which operator is performed first in an expression with more than one operators with different precedence is determined by operator precedence. when two operators of the same precedence appear in expression associativity is used. For example, ‘*’ and ‘/’ have the same precedence, so the expression “100 / 10 * 10” is worked as “(100 / 10) * 10”.

10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

In below table operators with the highest precedence appear at the top of the table and operators with the lowest precedence appear at the bottom.

Operator

Category
[ ] [ ]= Element reference, element set
** Exponentiation
!, ~, + Boolean NOT, bitwise complement, unary plus
*, /, % Multiplication, division, modulo (remainder)
+, – Addition (or concatenation), subtraction
< <, > > Bitwise shift-left (or append), bitwise shift-right
& Bitwise AND
|, ^ Bitwise OR, bitwise XOR
>, >=, <, <= Ordering
<=>, ==, ===, !=, =~, !~ Equality, pattern matching, comparison
&& Logical AND
|| Boolean OR
| | Logical OR
.., …= Range creation and Boolean flip-flops
?, : Conditional
modifier-rescue Exception-handling modifier
=, +=, -=, etc. Assignment
defined? Test variable definition and type
not Boolean NOT (low precedence)
or, and Boolean OR, Boolean AND
modifier-if, modifier-unless, modifier-while, modifier-until Conditional and loop modifiers
begin/end blocks

Below is the example of operator Precedence.
Example :





Output :

Value of a + b * c / d is : 50

In above example, multiply and divide have equal precedence. so it will work like 20 + (10 * 15) /5. firstly multiply perform than divide after that addition because addition has low precedence.


Previous Article
Next Article

Similar Reads

Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Operator Overloading in Ruby
Ruby permits operator overloading, allowing one to define how an operator shall be used in a particular program. For example a '+' operator can be define in such a way to perform subtraction instead addition and vice versa. The operators that can be overloaded are +, -, /, *, **, %, etc and some operators that can not be overloaded are &amp;, &amp;
5 min read
Private Classes in Ruby
The concept of private, protected and public methods in Ruby is a bit different than it other languages like Java. In Ruby, it is all about which class the person is calling, as classes are objects in ruby. Private Class When a constant is declared private in Ruby, it means this constant can never be called with an explicit receiver, a private cons
3 min read
Ruby | Matrix cofactor() function
The cofactor() is an inbuilt method in Ruby returns the cofactor of the given index. Syntax: mat1.cofactor(i, j) Parameters: The function needs two index i and j, whose cofactor is to be returned. Return Value: It returns the cofactor of the given index. Example 1: # Ruby program for cofactor() method in Matrix # Include matrix require &quot;matrix
1 min read
Ruby Integer odd? function with example
The odd? function in Ruby returns a boolean value. It returns true if the number is odd, else it returns false. Syntax: number.odd? Parameter: The function takes the integer which is to be checked for odd or not. Return Value: The function returns a boolean value which determines if the value is odd or not. Example #1: # Ruby program of integer odd
1 min read
Ruby Integer div() function with example
The div() function in Ruby returns the integer division of two numbers. Syntax: (number1).div(number2) Parameter: The function needs two numbers number1 and number2, where number1 is the dividend and number2 is the divisor. Return Value: The function returns the integer division of two numbers. Example #1: # Ruby program of Integer div() function #
1 min read
Ruby | Symbol to_s function
Symbol#to_s() : to_s() is a Symbol class method which returns string representation of the symbol object. Syntax: Symbol.to_s() Parameter: Symbol values Return: string representation of the symbol object. Example #1 : # Ruby code for Symbol.to_s() method # declaring Symbol a = :aBcDeF # declaring Symbol b = :&quot;\u{e4 f6 fc}&quot; # declaring Sym
1 min read
Ruby | Set replace() function
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after rep
1 min read
Ruby | Set reset() function
The reset() is an inbuilt method in Ruby resets the internal state after modification to existing elements and returns self. The elements will be reindexed and deduplicated. Syntax: s1.reset() Parameters: The function does not accepts any parameter. Return Value: It returns self . Example 1: # Ruby program to illustrate # the reset() method # requi
1 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Ruby | at() function
The at() function in Ruby is used to return the element of the specified index. Index 0 is for the first element of the array, 1 for the second element of the array and so on. The negative index counts from the end of the input array. Syntax: Array.at(Index) Here Array is the input array of elements at which at() function is called. Parameters: Ind
2 min read
Ruby Float to_r() method with example
Float to_r() is a float class method which return a rational form of the float value. Syntax: float.to_r() Parameter: float value as argument Return: Rational form representation of the float value. Example #1 : # Ruby code for to_r() method # Initializing values a = 2.0 b = 9.99991 # Printing result puts &quot;Rational form of a : #{a.to_r}\n\n
1 min read
Ruby | Keywords
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects or as constants. Doing this may result in compile-time error. Example: # Ruby program to illustrate Keywords #!/usr/bin/ruby # here 'if' is a k
4 min read
Ruby | Exception Class and its Methods
An exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at runtime, that disrupts the normal flow of the program’s instructions. In Ruby, descendants of an Exception class are used to interface between raise methods and rescue statements in the begin or end blocks. Exception objects carry information ab
3 min read
Ruby | String inspect Method
inspect is a String class method in Ruby which is used to return a printable version of the given string, surrounded by quote marks, with special characters escaped. Syntax: str.inspect Parameters: Returns: Here, str is the given string. Example 1: # Ruby program to demonstrate # the inspect method # Taking a string and # using the method puts
1 min read
Ruby | String hex Method
hex is a String class method in Ruby which is used to treats the leading characters from the given string as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. Syntax: str.hex Parameters: Here, str is the given string. Returns: A corresponding number. Example 1:
1 min read
Ruby | String include? Method
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str.include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false. Example 1: # Ruby program to demonstrate # the include? method #
1 min read
Ruby | Hash each_pair() function
Hash#each_pair() is a Hash class method which finds the nested value which calls block once for each pair in hash by passing the key_value pair as parameters. Syntax: Hash.each_pair() Parameter: Hash values Return: calls block once for key_value pair in hash with key_value pair as parameter otherwise Enumerator if no argument is passed. Example #1
2 min read
The yield Keyword in Ruby
We can send a block to our method and it can call that block multiple times. This can be done by sending a proc/lambda, but is easier and faster with yield. During a method invocation The yield keyword in corporation with a block allows to pass a set of additional instructions. When yield is called in side a method then method requires a block with
2 min read
Ruby | Struct to_s() function
The to_s() is an inbuilt method in Ruby that returns a string with the value of the particular struct. Syntax: struct_name.to_s() Parameters: The function does not accepts any parameter. Return Value: It returns the value of struct. Example 1: # Ruby program for to_s method in struct # Include struct animals = Struct.new(:name, :speciality , :found
1 min read
Ruby | Date === function
Date#===() is a Date class method which checks whether the two Date objects are equal. Syntax: Date.===() Parameter: Date values Return: true if two date objects are equal otherwise return false Example #1 : # Ruby code for Date.===() method # loading date require 'date' # declaring Date a = Date.new(2019, 1, 1) # declaring Date b = Date.jd(2452004
2 min read
Ruby | Date &lt;=&gt;() method
Date#&lt;=&gt; is a Date class method which compares two date objects. Syntax: Date&lt;=&gt;b Parameter: Date values Return: 1 if a is greater than b -1 if a is less than b 0 if a equals b Example #1 : # Ruby code for Date.&lt;=&gt;() method # loading date require 'date' # declaring Date a = Date.new(2019, 1, 1) # declaring Date b = Date.jd(2452004
2 min read
Ruby | Date - function
Date#-() is a Date class method which returns the difference between two dates if the object is date type object. Syntax: Date.-() Parameter: Date values Return: difference between two dates Example #1 : # Ruby code for Date.-() method # loading date require 'date' # declaring Date a = Date.new(2019, 1, 1) # declaring Date b = Date.jd(2452004) # de
2 min read
Ruby | Date + method
Date#+() is a Date class method which adds a numeric value to the date and returns a date object pointing other days after self. Syntax: Date.+() Parameter: Date values Return: date object pointing other days after self after adding a numeric value to the date. Example #1 : # Ruby code for Date.+() method # loading date require 'date' # declaring D
2 min read
Ruby | Complex + method
Complex#+() : +() is a Complex class method which performs the addition operation. Syntax: Complex.+()Parameter: Complex valuesReturn: Addition Operation on complex values. Example #1 : C/C++ Code # Ruby code for Complex.+() method # declaring Complex value a = Complex(200) # declaring Complex value b = Complex(-1, 4) # declaring Complex value c =
1 min read
Ruby | Loops (for, while, do..while, until)
Looping in programming languages is a feature which clears the way for the execution of a set of instructions or functions repeatedly when some of the condition evaluates to true or false. Ruby provides the different types of loop to handle the condition based situation in the program to make the programmers task simpler. The loops in Ruby are : wh
5 min read
Ruby | Array cycle() operation
Array#cycle() : cycle() is a Array class method which returns the array by calling the given block each time for every element in the array 'n' no. of times and if 'nil' is given, then it will call it for forever. Syntax: Array.cycle() Parameter: block - condition n - number of times to call the block Return: array by calling the given block each t
2 min read
Ruby | Array delete() operation
Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. Syntax: Array.delete() Parameter: obj - specific element to delete Return: last deleted values from the array. Code #1 : Example for delete() method C/C++ Code # Ruby code for delete(
2 min read
Ruby | Array delete_at() operation
Array#delete_at() : delete_at() is a Array class method which deletes the arrays elements at the mentioned index. Syntax: Array.delete_at() Parameter: index - value at specific index value to delete Return: deleted element, nil - if the index value is out of range Code #1 : Example for delete_at() method C/C++ Code # Ruby code for delete_at() metho
2 min read
Ruby | Array delete_if() operation
Array#delete_if() : delete_if() is a Array class method which deletes the arrays elements for which the block condition satisfies. Syntax: Array.delete_if() Parameter: block - condition for deleting the elements. Return: array after deleting the elements Code #1 : Example for delete_if() method C/C++ Code # Ruby code for delete_if() method # declar
1 min read
Article Tags :
three90RightbarBannerImg