Skip to content

Commit

Permalink
finished working program assumptions with proc functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobAlexander committed Sep 28, 2016
1 parent 1060e7a commit 1654d0a
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ def expect(obj)
end

def eq(val)
# Why it's so difficult ?
# Proc objects are blocks of code that have been bound to a set of local variables.
# Once bound, the code may be called in different contexts and still access those variables.
#
# So everything because of that different context !

# obj is an initialized value from "call" in "to" method, and we are expecting that value by this syntax
-> (obj) { obj == val }
# Ps. it's not a shortcode of regular proc function!
# Okay, it's proc because it know about local variables in his context
# but it expecting too that declared variables like lambda!
# So that's more like a mix of proc and lambda object - super object-function ? : )
end

def include(val)
-> (obj) { obj.include? val }
end

end
Expand All @@ -33,10 +49,25 @@ def initialize(object)
@object = object
end

def to(*)
def to(func)
# Here we are calling expected "method with was proc function -> eq(4)" inside our method "to"
# While calling method eq we send there our @object
# So one more time:
# We are saying hey eq! I know you are a proc function, so now do your job and take this @object as a initialized value

if func.call(@object)
print "."
else
print "F"
end
end

def not_to(*)
def not_to(func)
if !func.call(@object)
print "."
else
print "F"
end
end

end
Expand All @@ -45,5 +76,6 @@ def not_to(*)
describe "Amazing RSpeclike example" do
it "works!" do
expect(2+2).to eq(4)
expect(["a","b","c","d"]).to include("bb")
end
end
end

0 comments on commit 1654d0a

Please sign in to comment.