RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Similar documents
GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

JAVA OPERATORS GENERAL

Operators. Java operators are classified into three categories:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Here n is a variable name. The value of that variable is 176.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

A flow chart is a graphical or symbolic representation of a process.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 3: Operators, Expressions and Type Conversion

Operators in C. Staff Incharge: S.Sasirekha

Department of Computer Science

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators & Expressions

SECTION II: LANGUAGE BASICS

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Expressions and Precedence. Last updated 12/10/18

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

ECEN 468 Advanced Logic Design

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

The Java language has a wide variety of modifiers, including the following:

Computers Programming Course 6. Iulian Năstac

JAVASCRIPT - OPERATORS

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CT 229. Java Syntax 26/09/2006 CT229

Chap 6 - Introduction to HDL (b)

Prof. Navrati Saxena TA: Rochak Sachan

ISA 563 : Fundamentals of Systems Programming

Arithmetic Operators. Portability: Printing Numbers

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Lecture 3 Tao Wang 1

CSC 1214: Object-Oriented Programming

Informatics Ingeniería en Electrónica y Automática Industrial

Chapter 4: Basic C Operators

Quick Reference Guide

UNIT- 3 Introduction to C++

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Prefix/Infix/Postfix Notation

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Language Reference Manual simplicity

Expression and Operator

DEPARTMENT OF MATHS, MJ COLLEGE

Fundamentals of Programming

Fundamental of Programming (C)

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Information Science 1

Operators and Expressions

Chapter 4. Operations on Data

Introduction to Digital VLSI Design מבוא לתכנון VLSI ספרתי

Lesson 38: Conditionals #2 (W11D3)

Course Topics - Outline

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators in java Operator operands.

.Net Technologies. Components of.net Framework

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Variables and Operators 2/20/01 Lecture #

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

C/C++ Programming for Engineers: Working with Integer Variables

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Chapter 12 Variables and Operators

Bits, Words, and Integers

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Logical and Bitwise Expressions

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

GAS Tutorial - 6. Expression

Quick Reference Guide

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Operators And Expressions

In Delphi script, when values are assigned to variables, the colon-equal operator is used; :=

Lab 4: Arithmetic Logic Unit (ALU)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CA4003 Compiler Construction Assignment Language Definition

Fundamental of C programming. - Ompal Singh

Learning the Language - V

Expressions. Operands. Operators

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

ME 461 C review Session Fall 2009 S. Keres

Data Types and Variables in C language

Arithmetic and Bitwise Operations on Binary Data

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

More Programming Constructs -- Introduction

Chapter 17. Fundamental Concepts Expressed in JavaScript

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Computer Components. Software{ User Programs. Operating System. Hardware

Transcription:

http://www.tutorialspoint.com/ruby/ruby_operators.htm RUBY OPERATORS Copyright tutorialspoint.com Ruby supports a rich set of operators, as you'd expect from a modern language. Most operators are actually method calls. For example, a + b is interpreted as a.+b, where the + method in the object referred to by variable a is called with b as its argument. For each operator + /, there is a corresponding form of abbreviated assignment operator + = = etc. Ruby Arithmetic Operators: + Addition - Adds values on either side of the operator - Subtraction - Subtracts right hand operand from left hand operand * Multiplication - Multiplies values on either side of the operator / Division - Divides left hand operand by right hand operand % Modulus - Divides left hand operand by right hand operand and returns remainder ** Exponent - Performs exponential power calculation on operators a + b will give 30 a - b will give -10 a * b will give 200 b / a will give 2 b % a will give 0 a**b will give 10 to the power 20 Ruby Comparison Operators: == Checks if the value of two operands are equal or not, if yes then condition becomes true.!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes a == b is not true. a! = b is true. a > b is not true. a < b is true. a >= b is not true.

true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. <=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and - 1 if first operand is less than the second. === Used to test equality within a when clause of a case statement. a <= b is true. a <=> b returns -1. 1...10 === 5 returns true..eql? equal? True if the receiver and argument have both the same type and equal values. True if the receiver and argument have the same object id. 1 == 1.0 returns true, but 1.eql?1.0 is false. if aobj is duplicate of bobj then aobj == bobj is true, a.equal?bobj is false but a.equal?aobj is true. Ruby Assignment Operators: = Simple assignment operator, Assigns values from right side operands to left side operand += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand **= Exponent AND assignment operator, Performs exponential power calculation on operators and assign value to the left operand c = a + b will assigne value of a + b into c c += a is equivalent to c = c + a c -= a is equivalent to c = c - a c *= a is equivalent to c = c * a c /= a is equivalent to c = c / a c %= a is equivalent to c = c % a c **= a is equivalent to c = c ** a Ruby Parallel Assignment:

Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code. For example: a = 10 b = 20 c = 30 may be more quickly declared using parallel assignment: a, b, c = 10, 20, 30 Parallel assignment is also useful for swapping the values held in two variables: a, b = b, c Ruby Bitwise Operators: Bitwise operator works on bits and perform bit by bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows: a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 There are following Bitwise operators supported by Ruby language & Binary AND Operator copies a bit to the result if it exists in both operands. a & b will give 12, which is 0000 1100 Binary OR Operator copies a bit if it exists in either operand. a b will give 61, which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. a b will give 49, which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a will give -61, which is 1100 0011 in 2's complement form due to a signed binary number. a << 2 will give 240, which is 1111 0000 a >> 2 will give 15, which is 0000 1111 Ruby Logical Operators:

There are following logical operators supported by Ruby language and or && Called Logical AND operator. If both the operands are true, then the condition becomes true. Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true. Called Logical AND operator. If both the operands are non zero, then the condition becomes true. aandb is true. aorb is true. a && b is true. Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true.! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. a b is true.! a && b is false. not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. not a && b is false. Ruby Ternary operator: There is one more operator called Ternary Operator. This first evaluates an expression for a true or false value and then execute one of the two given statements deping upon the result of the evaluation. The conditional operator has this syntax:? : Conditional Expression If Condition is true? Then value X : Otherwise value Y Ruby Range operators: Sequence ranges in Ruby are used to create a range of successive values - consisting of a start value, an value and a range of values in between. In Ruby, these sequences are created using the ".." and "..." range operators. The two-dot form creates an inclusive range, while the three-dot form creates a range that excludes the specified high value... Creates a range from start point to point inclusive... Creates a range from start point to point exclusive 1..10 Creates a range from 1 to 10 inclusive 1...10 Creates a range from 1 to 9

Ruby defined? operators: defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined. It returns a description string of the expression, or nil if the expression isn't defined. There are various usage of defined? operator: Usage 1 defined? variable # True if variable is initialized foo = 42 defined? foo defined? $_ defined? bar # => "local-variable" # => "global-variable" # => nil (undefined) Usage 2 defined? method_call # True if a method is defined defined? puts defined? puts(bar) defined? unpack # => "method" # => nil (bar is not defined here) # => nil (not defined here) Usage 3 # True if a method exists that can be called with super user defined? super defined? super defined? super # => "super" (if it can be called) # => nil (if it cannot be) Usage 4 defined? yield # True if a code block has been passed defined? yield defined? yield # => "yield" (if there is a block passed) # => nil (if there is no block) Ruby dot "." and double Colon "::" Operators: You call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons. The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module. Remember: in Ruby, classes and methods may be considered constants too. You need just to prefix the :: Const_name with an expression that returns the appropriate class or module object.

If no prefix expression is used, the main Object class is used by default. Here are two examples: MR_COUNT = 0 # constant defined on main Object class module Foo MR_COUNT = 0 ::MR_COUNT = 1 # set global count to 1 MR_COUNT = 2 # set local count to 2 puts MR_COUNT # this is the global constant puts Foo::MR_COUNT # this is the local "Foo" constant Second Example: CONST = ' out there' class Inside_one CONST = proc {' in there'} def where_is_my_const ::CONST + ' inside one' class Inside_two CONST = ' inside two' def where_is_my_const CONST puts Inside_one.new.where_is_my_CONST puts Inside_two.new.where_is_my_CONST puts Object::CONST + Inside_two::CONST puts Inside_two::CONST + CONST puts Inside_one::CONST puts Inside_one::CONST.call + Inside_two::CONST Ruby Operators Precedence The following table lists all operators from highest precedence to lowest. Method Operator Description Yes :: Constant resolution operator Yes [ ] [ ]= Element reference, element set Yes ** Exponentiation raisetothepower Yes! ~ + - Not, complement, unary plus and minus methodnamesforthelasttwoare + @ and @ Yes * / % Multiply, divide, and modulo Yes + - Addition and subtraction Yes >> << Right and left bitwise shift Yes & Bitwise 'AND' Yes ^ Bitwise exclusive `OR' and regular `OR' Yes <= < > >= Comparison operators Yes <=> == ===!= =~!~ Equality and pattern match operators! = and! maynotbedefinedasmethods && Logical 'AND'

Logical 'OR'..... Range inclusiveandexclusive? : Ternary if-then-else = %= { /= -= += = &= >>= <<= *= &&= = **= defined? not or and Assignment Check if specified symbol defined Logical negation Logical composition NOTE: Operators with a Yes in the method column are actually methods, and as such may be overridden. Processing math: 100%