-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathVARIABLES IN RUBY
120 lines (103 loc) · 5.32 KB
/
VARIABLES IN RUBY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Naming things
In order to refer to the “things” (objects) that our program deals with we want to assign names to them.
Every practical programming language has a feature to do this, called variables. This is basically the same concept that you might know from math, although in Ruby there are different kinds of variables (you will get to know another one in a couple chapters).
We’ll discuss this concept quickly because you already need to know it in the next chapter, and the respective exercises.
If some of this seems rather abstract to you, don’t fret. It will become very practical, and you won’t even think a lot about it any more, as soon as you actually work with variables while learning other things.
Alright. Let’s jump right in:
In Ruby you can assign a name to something (an object) by using the so called assignment operator =. Like so:
number = 1
This will assign the name number to the “thing” (object) that is the number 1. From now on we can refer to this object by using the name number. For example the following code would output the number 1:
A name on the left side of the assignment operator = is assigned to the object on the right side.
number = 1
puts number
One important thing to note here is that a variable is not a “thing” an object by itself. Instead it’s just a name for an actual object. In our example the number 1 is an object, while number is a name for it because we’ve assigned it.
A variable itself is not a “thing”. It’s just a name for a thing (an object).
You can think of it like a post-it note with the name number written on it, and stuck on the actual thing, which is an object (in this case, a number).
Imagine you were in the middle of learning some Spanish, and sticked post-its onto things in your apartment: the name nevara onto the refridgerator, cama onto your bed, and puerta del baño onto the bathroom door.
Now, whenever you use one of these terms, as in abrir la nevera (open the refridgerator) in order to learn the language and rehearse vocabulary, you’ll obviously refer to the object, and open its actual, physical door.
That’s pretty much how variable assignment works in Ruby. There’s a “thing”, the object on the right side of the assignment operator =, and the name on the left side is being assigned to it.
Whenever we use a variable name that has been defined before we refer to the actual object that it has been assigned to. E.g. on the second line of the code example above number refers to the actual object, the number 1 that it has been assigned to on the first line. Therefore puts number outputs the number.
You can pick whatever variable names you want, they’re just names, like post-it notes stuck onto actual objects.
Since names are just names, the following examples would do exactly the same:
a = 1
puts a
large_number = 1
puts large_number
apples = 1
puts apples
However, also note that it makes sense to try and pick names that reveal your intention. The chapter Using the right words at the end of this book will talk more about this. Feel free to jump ahead if you are curious. In short, the first example using a as a name would be frowned upon because it’s wasting the opportunity to use a meaningful name. The second and third examples are just trying to be stupid, and pick names that don’t match the “thing” (the object, number 1) at all.
## Types of variables in ruby
In ruby you can define variables in the following category
1. Global
2. Instance
3. Class
4. Local
5. Ruby Constants
**Global variables** - Their name starts with `$`
```
$global_var = 10
class C1
def show_global
puts "C1: The global variable- #{$global_var}"
end
end
class C2
def change_and_show_global
$global_var = -9
puts "C2: The global variable- #{$global_var}"
end
end
C1.new.show_global # => 10
C2.new.change_and_show_global # => -9
C1.new.show_global # => -9
```
Use of global variables is discouraged as they value can be changed anywhere and can have sideffects.
**Instance variable** - Their name starts with `@` sign. Uninitialized instance variables have the value _nil_
```
class Developer
def initialize(name, skill)
@name = name
@skill = skill
end
def introduction
puts "I am #{@name}, my skills includes {@skill}"
end
end
vikas = developer.new('vikas', 'ruby')
vikas.introduction # => I am vikas, my skills includes ruby
```
**Class variables** - They starts with `@@`
```
class Developer
@@dev_count = 0 # Class variable
def initialize(name, skill)
@@dev_count += 1
@name = name
@skill = skill
end
def introduction
puts "I am #{@name}, my skills includes {@skill}"
end
def self.dev_count
puts @@dev_count
end
end
vikas = developer.new('vikas', 'ruby')
karan = developer.new('vikas', 'javascript')
Developer.dev_count # => 2
```
**Local Variables** - Their name starts with a lowercase letter or `_`
They have a scope within a `class`, `module`, `def-end`, or `do-end` block.
**Ruby Constants** - Their name starts with an uppercase letter. They are defined to indicate a value that should not be altered in the program.
```
class Vote
MIN_AGE = 18
def cast_vote(voter, party)
if voter.age >= Vote::MIN_AGE
party.vote + = 1
# cast +1 vote to the `party`
end
end
end
# Here MIN_AGE is defined as constant and its value should not be altered anywhere in the program
```