SlideShare a Scribd company logo
"Variables, expressions, standard types"
или
то, что вы уже знаете, если делали коэны
$global_variables
Global variables begin with $
It is not recommended to use global variables. They make programs cryptic.
@instance_variables
class Customer
def initialize(id, name, addr)
@cust_id = id
@cust_name = name
@cust_addr = addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
@@class_variables
Class variables are shared among descendants of the class or module in which the class variables are
defined.
class Customer
@@no_of_customers = 0
…
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
cust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2 = Customer.new("2", "Poul", "New Empire road, Khandala")
cust1.total_no_of_customers() #=> Total number of customers: 1
cust2.total_no_of_customers() #=> Total number of customers: 2
local_variables
Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class,
module, def, or do to the corresponding end or from a block's opening brace to its close brace {}.
irb(main):011:0> class TestClass
irb(main):012:1> test_local_variable = 1
irb(main):013:1> def test_method
irb(main):014:2> puts test_local_variable
irb(main):015:2> end
irb(main):016:1> end
=> :test_method
irb(main):017:0> TestClass.new.test_method
NameError: undefined local variable or method `test_local_variable' for
#<TestClass:0x00007fc8be03a028>
from (irb):14:in `test_method'
from (irb):17
from /Users/yakavkrasnov/.rvm/rubies/ruby-2.4.2/bin/irb:11:in `<main>'
irb(main):018:0> class TestClass
irb(main):019:1> attr_reader :test_var
irb(main):020:1> def initialize(var)
irb(main):021:2> @test_var = var
irb(main):022:2> end
irb(main):023:1> def smth
irb(main):024:2> puts test_var
irb(main):025:2> end
irb(main):026:1> end
=> :smth
irb(main):027:0> TestClass.new('haha').smth
haha
=> nil
CONSTANTS
irb(main):028:0> class Example
irb(main):029:1> VAR1 = 100
irb(main):030:1> VAR2 = 200
irb(main):031:1> def show
irb(main):032:2> puts "Value of first Constant is #{VAR1}"
irb(main):033:2> puts "Value of second Constant is #{VAR2}"
irb(main):034:2> end
irb(main):035:1> end
irb(main):036:0> object = Example.new()
=> #<Example:0x00007fc8be1d28e0>
irb(main):037:0> object.show
Value of first Constant is 100
Value of second Constant is 200
Ruby Pseudo-Variables
● self − The receiver object of the current method.
● true − Value representing true.
● false − Value representing false.
● nil − Value representing undefined.
● __FILE__ − The name of the current source file.
● __LINE__ − The current line number in the source file.
Ruby expressions
Expressions are constructed from operands and operators. The operators of an
expression indicate which operations to apply to the operands. The order of
evaluation of operators in an expression is determined by the precedence and
associativity of the operators.
Category Symbol
Resolution, access operators :: .
Array operators [ ] [ ]=
Exponentiation **
Not, complement, unary plus, minus ! ~ + -
Multiply, divide, modulo * / %
Addition, substraction + -
Shift operators << >>
Bitwise and &
Bitwise or, logical or ^ |
Relational operators > >= < <=
Bitwise or, logical or ^ |
Equality, pattern match operators <=> == === != =~ !~
Logical and &&
Logical or ||
Range operators .. ...
Ternary ?:
Assignment operators = += -= *= **= /= %= &= |= ^= <<= >>= ||= &&=
Alternate negation not
Alternate logical or, and or and
Ruby concatenating strings
"Return ".+"of ".+ "the ".+"King"
Under the hood, the + operator is a Ruby method. The string
literal is an object. We call a method of an object using
the access . operator.
Use interpolation instead of concatenation.
@user_name = 'Толик'
puts "Hello, #{@user_name}" #=> "Hello, Толик"
Ruby increment, decrement operators
Ruby has no such operators:
x++, ++x, x--, --x
why?
irb(main):031:0> --2
=> 2
use +=, -=, *=, /=
Ruby arithmetic operators
irb(main):048:0> 5.div 2.0
=> 2
irb(main):049:0> 5.fdiv 2
=> 2.5
irb(main):050:0> 5.quo 2
=> 5/2
irb(main):051:0> 5.0.quo 2.0
=> 2.5
irb(main):052:0> 10 % 4
=> 2
irb(main):057:0> 2 / 3
=> 0
irb(main):058:0> 2 / 3.0
=> 0.6666666666666666
Ruby Boolean operators
irb(main):064:0> true && true
=> true
irb(main):065:0> true && false
=> false
irb(main):066:0> false && true
=> false
irb(main):067:0> false && false
=> false
irb(main):071:0* false || true
=> true
irb(main):072:0> false || false
=> false
irb(main):073:0> true || true
=> true
irb(main):074:0> true || false
=> true
Ruby Boolean operators
irb(main):060:0> true && "Hello, #{@user_name}"
=> "Hello, Толик"
irb(main):061:0> 0 || 1
=> 0
irb(main):062:0> !0
=> false
irb(main):077:0> false || "Hello, #{@user_name}"
=> "Hello, Толик"
Ruby elational Operators
Symbol Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Ruby associativity && Ruby range operators
a = b = c = d = 0
irb(main):089:0> print a, b, c, d
0000
irb(main):095:0> (1..3).to_a
=> [1, 2, 3]
irb(main):096:0> (1...3).to_a
=> [1, 2]
Ruby ternary operator
cond-exp ? exp1 : exp2
age = 32
adult = age >= 18 ? true : false

More Related Content

PDF
JavaScript Fundamentals with Angular and Lodash
Bret Little
 
PDF
Lodash js
LearningTech
 
PDF
Hidden Gems in Swift
Netguru
 
PPTX
Python: Basic Inheritance
Damian T. Gordon
 
PPTX
Php & my sql
Norhisyam Dasuki
 
PDF
Introduction to Clean Code
Julio Martinez
 
PPTX
Crafting beautiful software
Jorn Oomen
 
PPSX
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
JavaScript Fundamentals with Angular and Lodash
Bret Little
 
Lodash js
LearningTech
 
Hidden Gems in Swift
Netguru
 
Python: Basic Inheritance
Damian T. Gordon
 
Php & my sql
Norhisyam Dasuki
 
Introduction to Clean Code
Julio Martinez
 
Crafting beautiful software
Jorn Oomen
 
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 

What's hot (20)

PDF
Your code sucks, let's fix it
Rafael Dohms
 
PDF
Codeware
Uri Nativ
 
PDF
Swift 3.0 の新しい機能(のうちの9つ)
Tomohiro Kumagai
 
PDF
Specs2
Piyush Mishra
 
PDF
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
PDF
Javascript essentials
Bedis ElAchèche
 
PPTX
Js types
LearningTech
 
PDF
Java script obfuscation
n|u - The Open Security Community
 
PPTX
Objective-c Runtime
Pavel Albitsky
 
PDF
Some OOP paradigms & SOLID
Julio Martinez
 
PDF
PHP Unit 4 arrays
Kumar
 
PPT
Php Chapter 1 Training
Chris Chubb
 
PDF
06 ruby variables
Walker Maidana
 
PPTX
Groovy grails types, operators, objects
Husain Dalal
 
PDF
Bottom Up
Brian Moschel
 
PPT
Javascript
Manav Prasad
 
PPTX
Class 8 - Database Programming
Ahmed Swilam
 
PDF
Array String - Web Programming
Amirul Azhar
 
PDF
Headless Js Testing
Brian Moschel
 
Your code sucks, let's fix it
Rafael Dohms
 
Codeware
Uri Nativ
 
Swift 3.0 の新しい機能(のうちの9つ)
Tomohiro Kumagai
 
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Javascript essentials
Bedis ElAchèche
 
Js types
LearningTech
 
Java script obfuscation
n|u - The Open Security Community
 
Objective-c Runtime
Pavel Albitsky
 
Some OOP paradigms & SOLID
Julio Martinez
 
PHP Unit 4 arrays
Kumar
 
Php Chapter 1 Training
Chris Chubb
 
06 ruby variables
Walker Maidana
 
Groovy grails types, operators, objects
Husain Dalal
 
Bottom Up
Brian Moschel
 
Javascript
Manav Prasad
 
Class 8 - Database Programming
Ahmed Swilam
 
Array String - Web Programming
Amirul Azhar
 
Headless Js Testing
Brian Moschel
 
Ad

Similar to Variables, expressions, standard types (20)

PDF
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
rahulfancycorner21
 
PPTX
Introduction to Client-Side Javascript
Julie Iskander
 
PPTX
Ruby from zero to hero
Diego Lemos
 
PPTX
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
 
PDF
Ruby on Rails 中級者を目指して - 大場寧子
Yasuko Ohba
 
PDF
Ruby on Rails ステップアップ講座 - 大場寧子
Yasuko Ohba
 
PDF
Attributes Unwrapped: Lessons under the surface of active record
.toster
 
PDF
Introduction to Active Record at MySQL Conference 2007
Rabble .
 
PDF
Object Trampoline: Why having not the object you want is what you need.
Workhorse Computing
 
ODP
What I Love About Ruby
Keith Bennett
 
PDF
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Rabble .
 
PPTX
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
KEY
Ruby/Rails
rstankov
 
PDF
A limited guide to intermediate and advanced Ruby
Vysakh Sreenivasan
 
PPTX
The Style of C++ 11
Sasha Goldshtein
 
PDF
Metaprogramovanie #1
Jano Suchal
 
PDF
How to write Ruby extensions with Crystal
Anna (gaar4ica) Shcherbinina
 
KEY
Why ruby
rstankov
 
PPTX
Ruby on rails tips
BinBin He
 
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
rahulfancycorner21
 
Introduction to Client-Side Javascript
Julie Iskander
 
Ruby from zero to hero
Diego Lemos
 
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
 
Ruby on Rails 中級者を目指して - 大場寧子
Yasuko Ohba
 
Ruby on Rails ステップアップ講座 - 大場寧子
Yasuko Ohba
 
Attributes Unwrapped: Lessons under the surface of active record
.toster
 
Introduction to Active Record at MySQL Conference 2007
Rabble .
 
Object Trampoline: Why having not the object you want is what you need.
Workhorse Computing
 
What I Love About Ruby
Keith Bennett
 
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Rabble .
 
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
Ruby/Rails
rstankov
 
A limited guide to intermediate and advanced Ruby
Vysakh Sreenivasan
 
The Style of C++ 11
Sasha Goldshtein
 
Metaprogramovanie #1
Jano Suchal
 
How to write Ruby extensions with Crystal
Anna (gaar4ica) Shcherbinina
 
Why ruby
rstankov
 
Ruby on rails tips
BinBin He
 
Ad

More from Rubizza (9)

PDF
Intoduction to React
Rubizza
 
PDF
Linux commands-effectiveness
Rubizza
 
PDF
Sinatra
Rubizza
 
PDF
Catch and Throw in Ruby
Rubizza
 
PDF
Git
Rubizza
 
PDF
Hangout Utche #6. "Rambovidnaya problema"
Rubizza
 
PDF
Hangout Utche #6. Math Thinking
Rubizza
 
PDF
Rubizza #1 | Special Lecture. Vim
Rubizza
 
PPTX
Rubizza #1 Lecture Ruby OOP
Rubizza
 
Intoduction to React
Rubizza
 
Linux commands-effectiveness
Rubizza
 
Sinatra
Rubizza
 
Catch and Throw in Ruby
Rubizza
 
Git
Rubizza
 
Hangout Utche #6. "Rambovidnaya problema"
Rubizza
 
Hangout Utche #6. Math Thinking
Rubizza
 
Rubizza #1 | Special Lecture. Vim
Rubizza
 
Rubizza #1 Lecture Ruby OOP
Rubizza
 

Recently uploaded (20)

PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Doc9.....................................
SofiaCollazos
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Software Development Methodologies in 2025
KodekX
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 

Variables, expressions, standard types

  • 1. "Variables, expressions, standard types" или то, что вы уже знаете, если делали коэны
  • 2. $global_variables Global variables begin with $ It is not recommended to use global variables. They make programs cryptic.
  • 3. @instance_variables class Customer def initialize(id, name, addr) @cust_id = id @cust_name = name @cust_addr = addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end end
  • 4. @@class_variables Class variables are shared among descendants of the class or module in which the class variables are defined. class Customer @@no_of_customers = 0 … def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end cust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2 = Customer.new("2", "Poul", "New Empire road, Khandala") cust1.total_no_of_customers() #=> Total number of customers: 1 cust2.total_no_of_customers() #=> Total number of customers: 2
  • 5. local_variables Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}. irb(main):011:0> class TestClass irb(main):012:1> test_local_variable = 1 irb(main):013:1> def test_method irb(main):014:2> puts test_local_variable irb(main):015:2> end irb(main):016:1> end => :test_method irb(main):017:0> TestClass.new.test_method NameError: undefined local variable or method `test_local_variable' for #<TestClass:0x00007fc8be03a028> from (irb):14:in `test_method' from (irb):17 from /Users/yakavkrasnov/.rvm/rubies/ruby-2.4.2/bin/irb:11:in `<main>'
  • 6. irb(main):018:0> class TestClass irb(main):019:1> attr_reader :test_var irb(main):020:1> def initialize(var) irb(main):021:2> @test_var = var irb(main):022:2> end irb(main):023:1> def smth irb(main):024:2> puts test_var irb(main):025:2> end irb(main):026:1> end => :smth irb(main):027:0> TestClass.new('haha').smth haha => nil
  • 7. CONSTANTS irb(main):028:0> class Example irb(main):029:1> VAR1 = 100 irb(main):030:1> VAR2 = 200 irb(main):031:1> def show irb(main):032:2> puts "Value of first Constant is #{VAR1}" irb(main):033:2> puts "Value of second Constant is #{VAR2}" irb(main):034:2> end irb(main):035:1> end irb(main):036:0> object = Example.new() => #<Example:0x00007fc8be1d28e0> irb(main):037:0> object.show Value of first Constant is 100 Value of second Constant is 200
  • 8. Ruby Pseudo-Variables ● self − The receiver object of the current method. ● true − Value representing true. ● false − Value representing false. ● nil − Value representing undefined. ● __FILE__ − The name of the current source file. ● __LINE__ − The current line number in the source file.
  • 9. Ruby expressions Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators.
  • 10. Category Symbol Resolution, access operators :: . Array operators [ ] [ ]= Exponentiation ** Not, complement, unary plus, minus ! ~ + - Multiply, divide, modulo * / % Addition, substraction + - Shift operators << >> Bitwise and & Bitwise or, logical or ^ | Relational operators > >= < <= Bitwise or, logical or ^ | Equality, pattern match operators <=> == === != =~ !~ Logical and && Logical or || Range operators .. ... Ternary ?: Assignment operators = += -= *= **= /= %= &= |= ^= <<= >>= ||= &&= Alternate negation not Alternate logical or, and or and
  • 11. Ruby concatenating strings "Return ".+"of ".+ "the ".+"King" Under the hood, the + operator is a Ruby method. The string literal is an object. We call a method of an object using the access . operator. Use interpolation instead of concatenation. @user_name = 'Толик' puts "Hello, #{@user_name}" #=> "Hello, Толик"
  • 12. Ruby increment, decrement operators Ruby has no such operators: x++, ++x, x--, --x why? irb(main):031:0> --2 => 2 use +=, -=, *=, /=
  • 13. Ruby arithmetic operators irb(main):048:0> 5.div 2.0 => 2 irb(main):049:0> 5.fdiv 2 => 2.5 irb(main):050:0> 5.quo 2 => 5/2 irb(main):051:0> 5.0.quo 2.0 => 2.5 irb(main):052:0> 10 % 4 => 2 irb(main):057:0> 2 / 3 => 0 irb(main):058:0> 2 / 3.0 => 0.6666666666666666
  • 14. Ruby Boolean operators irb(main):064:0> true && true => true irb(main):065:0> true && false => false irb(main):066:0> false && true => false irb(main):067:0> false && false => false irb(main):071:0* false || true => true irb(main):072:0> false || false => false irb(main):073:0> true || true => true irb(main):074:0> true || false => true
  • 15. Ruby Boolean operators irb(main):060:0> true && "Hello, #{@user_name}" => "Hello, Толик" irb(main):061:0> 0 || 1 => 0 irb(main):062:0> !0 => false irb(main):077:0> false || "Hello, #{@user_name}" => "Hello, Толик"
  • 16. Ruby elational Operators Symbol Meaning < less than <= less than or equal to > greater than >= greater than or equal to
  • 17. Ruby associativity && Ruby range operators a = b = c = d = 0 irb(main):089:0> print a, b, c, d 0000 irb(main):095:0> (1..3).to_a => [1, 2, 3] irb(main):096:0> (1...3).to_a => [1, 2]
  • 18. Ruby ternary operator cond-exp ? exp1 : exp2 age = 32 adult = age >= 18 ? true : false