Created
January 10, 2020 10:22
-
-
Save abhionlyone/3739173db92f3d6fce267269cb700f43 to your computer and use it in GitHub Desktop.
Understanding functional programming in Ruby.
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
# Understanding FP in Ruby | |
class Counter | |
def initialize | |
@count = 0 | |
end | |
def call | |
@count += 1 | |
end | |
end | |
def counter | |
count = 0 | |
return Proc.new{ count+=1} | |
end | |
# The Class counter implements functionality of a counter using OOPS | |
# The method counter implements counter functionality using Functional Programming. | |
# Both Counter and counter implements similar functionality but uses different programming paradigm | |
counter1 = Counter.new | |
counter2 = Counter.new | |
counter3 = counter | |
counter4 = counter | |
counter1.call # 1 | |
counter1.call # 2 | |
counter2.call # 1 | |
counter2.call # 2 | |
counter3.call # 1 | |
counter3.call # 2 | |
counter4.call # 1 | |
counter4.call # 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment