forked from rabajaj0509/Ruby-concepts-step-by-step
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter8-Strings, Symbols and other scalar objects
- Loading branch information
1 parent
1dbda1c
commit d0b5c06
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Chapter8-Strings, Symbols and other scalar objects/symbols.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Symbols are immutable and unique. | ||
|
||
# When you asign a value to a variable | ||
# or constant or create a mothod or | ||
# class, the identifier goes to the Ruby's | ||
# internal symbol table. | ||
|
||
# symbols don't differ in any way from | ||
# immutable strings EXCEPT the object_id | ||
# stays static. | ||
|
||
>> class Dog | ||
>> def initialize(breed, name) | ||
>> # Instance variables | ||
>> @breed = breed | ||
>> @name = name | ||
>> end | ||
>> | ||
>> def bark | ||
>> puts 'Ruff! Ruff!' | ||
>> end | ||
>> | ||
>> def display | ||
>> puts "I am of #{@breed} breed and my name is #{@name}" | ||
>> end | ||
>> end | ||
=> :display | ||
|
||
>> d = Dog.new('Labrador', 'jounior') | ||
=> #<Dog:0x0055870a9d3fa0 @breed="Labrador", @name="junior"> | ||
|
||
>> d.method(:display).call | ||
I am of Labrador breed and my name is junior | ||
=> nil | ||
|