Skip to content

Commit

Permalink
Chapter1 - Objects, Methods and Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rabajaj0509 committed Aug 1, 2017
1 parent e391a6c commit 37fd43d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Chapter1-Objects and methods/illustration_of_local_variable.rb
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
16 changes: 16 additions & 0 deletions Chapter1-Objects and methods/multiple_arguments.rb
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.

10 changes: 10 additions & 0 deletions Chapter1-Objects and methods/respond_to.rb
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

0 comments on commit 37fd43d

Please sign in to comment.