0% found this document useful (0 votes)
7 views

Ruby - Variables

The document discusses different types of variables in Ruby including global, local, class, instance, and constant variables. It explains the scope of each variable type and provides examples of how to use variables in Ruby code. Practice problems demonstrate global, local, instance, and class variables. The exercises ask the reader to determine the output of code using variables with different scopes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Ruby - Variables

The document discusses different types of variables in Ruby including global, local, class, instance, and constant variables. It explains the scope of each variable type and provides examples of how to use variables in Ruby code. Practice problems demonstrate global, local, instance, and class variables. The exercises ask the reader to determine the output of code using variables with different scopes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Variables

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved


Outline
1. What is variable?
2. Ruby variables
3. Ruby variable scope
4. Practice
5. Exercise

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 2


1. What is variable?

video

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 3


2. Ruby variables
❖ Global variable - $ sign
➔ Available everywhere within your Ruby script
❖ Local variable
➔ It depends on the scope (create new scope with new class, new module,
new method)
❖ Class variable - @@ sign
➔ Available from the class definition and any sub-classes. Not available
from anywhere outside.
❖ Instance variable - @ sign
➔ Available only within a specific object, across all methods in a class
instance. Not available directly from class definitions.
❖ Constants - Begin with a capital letter

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 4


3. Ruby variable scope

Image reference: https://www.sitepoint.com/understanding-scope-in-ruby/

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 5


4. Practice - Global variables
$global = 0

class C
puts "in a class: #{$global}" #Result

def my_method in a class: 0


puts "in a method: #{$global}"
in a method: 0
$global = $global + 1
$global: 1, $other_global: 3
$other_global = 3
end
end

C.new.my_method

puts "$global: #{$global}, $other_global: #{$other_global}"

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 6


4. Practice - Local variable
color = "Red"
def method1
color = 192
puts("Color Value in method1 : ", color)
end
#Result
def method2 Color value in method1: 192
color = 255
puts("Color Value method2: ", color) Color value in method2: 255
end Color value in method1: 192
Color value outside methods : Red
method1
method2
method1
puts("Color Value outside methods : " + color)

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 7


4. Practice - Instance variable
class Student
def initialize student_id, student_name
@student_id = student_id
@student_name = student_name
end
#Result
Student Name and ID :
def show
1
puts "Student Name and ID : "
Sara
puts @student_id, @student_name
Student Name and ID :
end
2
end
Raju
Student.new(1, "Sara").show
Student.new(2, "Raju").show

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 8


4. Practice - Class variables
class Polygon
@@sides = 10
puts Polygon.sides
def set_sides sides
@@sides = sides p1 = Polygon.new
#Result
end p1.set_sides 20
10
puts p1.get_sides
20
def get_sides 30
@@sides p2 = Polygon.new
30
end p2.set_sides 30
30
puts p2.get_sides
def self.sides puts p1.get_sides
@@sides puts Polygon.sides
end
end

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 9


4. Practice - Constant variables
#Result
(irb):2: warning: already initialized constant NAME
(irb):1: warning: previous definition of NAME was
NAME = "LIENQUAN" here
NAME = "PUBG" => "PUBG"
(irb):2: warning: already initialized constant Name
Name = "LIENQUAN" (irb):1: warning: previous definition of Name was
Name = "PUBG" here
=> "PUBG"

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 10


5. Exercise
Look at the following programs...
x = 0
3.times do
x += 1
end
puts x y = 0
3.times do
y += 1
x = y
end
puts x

What does x print to the screen in each case? Do they both give errors? Are
the errors different? Why?
© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 11
5. Exercise
The first prints 3 to the screen.

The second throws an error undefined local variable or method because x is not
available as it is created within the scope of the do/end block.

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 12


References
❖ http://ruby-doc.org/
❖ http://annaershova.github.io/blog/2015/10/26/variable-in-ruby/
❖ http://rosettacode.org/wiki/99_Bottles_of_Beer#Ruby
❖ https://www.w3resource.com/ruby/ruby-variables-constants.php
❖ https://github.com/awesome-academy/RubyExample_TFW

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 13


Question & Answer?

© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 14


© 2022 By Sun* - Talent Incubator Vietnam Unit - All rights reserved 15

You might also like