Skip to content

Commit

Permalink
Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
msatmod committed Aug 30, 2022
1 parent cd640d3 commit dd23f34
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Day 19/Arity_method.rb
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"
103 changes: 103 additions & 0 deletions Day 19/Difference_between_Procs_&_Lambda.rb
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

0 comments on commit dd23f34

Please sign in to comment.