Skip to content

Instantly share code, notes, and snippets.

@memetical
Forked from anonymous/ex3
Created October 31, 2012 18:53
Show Gist options
  • Save memetical/3989084 to your computer and use it in GitHub Desktop.
Save memetical/3989084 to your computer and use it in GitHub Desktop.
exercise3
# Exercise: write a proc that provides content for the following html template:
def template
template =<<DELIM
<html>
<head>
#{yield :header}
</head>
<body>
#{yield}
<hr>
<p>
#{yield :footer}
</p>
</body>
</html>
DELIM
return template
end
content_definitions = proc {|x|
text = "<p> body text</p>"
if (x.eql?:"header")
text = "<title>The Title Defintion</title>"
end
if(x.eql?:"footer")
text = "<p>This could be a footer.</p>"
end
text
}
puts template &content_definitions
# procs are closures!
# that is, they preserve the variable bindings from the place where they are defined.
#extend create_counter such that it returns two procs, one for #increasing, one for decreasing the counter.
def create_counter
n = 0
proc { n+=1 }
end
first_counter =create_counter
puts "first_counter.call #{first_counter.call}"
puts "first_counter.call #{first_counter.call}"
second_counter =create_counter
puts "second_counter.call #{second_counter.call}"
puts "first_counter.call #{first_counter.call}"
puts "second_counter.call #{second_counter.call}"
puts "first_counter.call #{first_counter.call}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment