-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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,24 @@ | ||
# Using Arity method in procs | ||
|
||
# This method allows us to find how many arguments a proc object expects to receive | ||
|
||
my_proc = Proc.new{|x| "Filly " * x} | ||
|
||
puts "Hey Filly...! I need #{my_proc.arity} arguments" | ||
|
||
puts my_proc.call(5) | ||
|
||
|
||
my_proc = Proc.new{|x, y| "Filly " * (x + y) } | ||
|
||
puts "Hey Filly...! I need #{my_proc.arity} arguments" | ||
|
||
|
||
my_proc = Proc.new{|x, *rest| "#{x} and #{rest}"} | ||
|
||
puts "Hey Filly...! I need #{my_proc.arity} arguments" | ||
|
||
|
||
my_proc = Proc.new{|x, *rest| "#{x} and #{rest}"} | ||
|
||
puts "Hey Filly...! I need #{~my_proc.arity} arguments and rest are optional" |
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,103 @@ | ||
# Difference between Procs and Lambda | ||
|
||
# Program of a Proc | ||
|
||
def my_method | ||
|
||
puts "Before Proc" | ||
|
||
my_proc = proc{ | ||
|
||
puts "Inside Proc" | ||
} | ||
|
||
my_proc.call | ||
|
||
puts "After Proc" | ||
|
||
end | ||
|
||
my_method | ||
|
||
|
||
|
||
def my_method | ||
|
||
puts "Before Proc" | ||
|
||
my_proc = proc{ | ||
|
||
puts "Inside Proc" | ||
|
||
return | ||
} | ||
|
||
my_proc.call | ||
|
||
puts "After Proc" | ||
|
||
end | ||
|
||
my_method | ||
|
||
|
||
|
||
# Program of a Lambda | ||
|
||
def my_method | ||
|
||
puts "Before Proc" | ||
|
||
my_proc = lambda{ | ||
|
||
puts "Inside Proc" | ||
} | ||
|
||
my_proc.call | ||
|
||
puts "After Proc" | ||
|
||
end | ||
|
||
my_method | ||
|
||
|
||
def my_method | ||
|
||
puts "Before Proc" | ||
|
||
my_proc = lambda{ | ||
|
||
puts "Inside Proc" | ||
|
||
return | ||
} | ||
|
||
my_proc.call | ||
|
||
puts "After Proc" | ||
|
||
end | ||
|
||
my_method | ||
|
||
|
||
|
||
def my_method | ||
|
||
puts "Before Proc" | ||
|
||
my_proc = lambda{ | ||
|
||
puts "Inside Proc" | ||
|
||
break | ||
} | ||
|
||
my_proc.call | ||
|
||
puts "After Proc" | ||
|
||
end | ||
|
||
my_method |