Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Ruby_Variables.md #119

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 40 additions & 68 deletions Ruby_Variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,74 @@

There are 4 types of fundamental variables in ruby

1.-Global variables:
### 1. Global variables:

Global variables begin with $,
Uninitialized instance variables have the value nil
and produce warnings
Global variables begin with `$`. Uninitialized instance variables have the value `nil` and produce warnings.

```ruby
$global_variable = 10
puts "Global variable is $global_variable"
```

$global_variable = 10
puts "Global variable is $global_variable"
### 2. Instance Variables:

An instance variable has a name that begins with `@` and its scope is limited to the object to which reference self.

2.-Instance Variables:

An instance variable has a name that begins with @ and its scope is
limited to the object to which reference self


Two different objects, even when they belong to the same class, can have different values ​​in their instance variables

class Test

def set_example (n)

@example = n
Two different objects, even when they belong to the same class, can have different values in their instance variables.

```ruby
class Test
def set_example (n)
@example = n
end

def set_example2 (n)

@example2 = n


def set_example2 (n)
@example2 = n
end
end
```

end



3.-Ruby Class Variables:

Class variables begin with @@ and must be initialized
before they can be used in method definitions.


Referencing an uninitialized class variable produces an error. Class variables are shared
among descendants of the class or module in which the class variables are defined.

### 3. Ruby Class Variables:

Overriding class variables produce warnings
Class variables begin with `@@` and must be initialized before they can be used in method definitions.

Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.

class Workers

@@number_of_workers = 0

def initialize (name, id)
Overriding class variables produce warnings.

```ruby
class Workers
@@number_of_workers = 0

def initialize (name, id)
@cust_name = name
@cust_id = id

end

def display_details()

def display_details()
puts "Worker id #@cust_id"
puts "Worker name #@cust_name"

end


def total_workers()

def total_workers()
@@number_of_workers += 1
puts "Total is: #@@number_of_workers"

end

end
```

### 4. Ruby constants:

4.-Ruby constants:


Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside
a class or module can be accessed globally.

Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.

class Example

VAR1 = 100
VAR2 = 200

def show

puts "Value of first Constant is #{VAR1}"
puts "Value of second Constant is #{VAR2}"
```ruby
class Example
VAR1 = 100
VAR2 = 200

def show
puts "Value of first Constant is #{VAR1}"
puts "Value of second Constant is #{VAR2}"
end

end
```