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.
Chapter1 - Objects, Methods and Variables
- Loading branch information
1 parent
e391a6c
commit 37fd43d
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Chapter1-Objects and methods/illustration_of_local_variable.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,17 @@ | ||
#A local variable is only visible in a limited part of the program. | ||
|
||
|
||
def say_goodbye | ||
x = "Good Bye" | ||
puts x | ||
end | ||
|
||
def start_here | ||
x = "Hello" | ||
puts x | ||
say_goodbye | ||
puts "Let's check whether x remained the same ?" | ||
puts x | ||
end | ||
|
||
start_here |
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,16 @@ | ||
#It is possible to write a method that allows any number of arguments. | ||
#To do this, put a star(an asterish *) in front of a single argument name | ||
|
||
|
||
obj = Object.new | ||
|
||
def two_or_more(a,b,*c) | ||
puts "Agrs are : " | ||
p a, b, c | ||
end | ||
|
||
two_or_more(1,2,3,4,5,6) | ||
|
||
#The variable 'x' will be assigned an array of values corresponding to the | ||
#arguments that were sent. | ||
|
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,10 @@ | ||
#You can determine in advance whether the object knows how to handle the | ||
#message you want to send it, by using the respond_to? method. | ||
|
||
obj = Object.new | ||
if obj.respond_to?("talk") | ||
obj.talk | ||
else | ||
puts "Sorry, the object doesn't understand the 'talk' message" | ||
end | ||
|