Skip to content

Instantly share code, notes, and snippets.

/ex3

Created October 31, 2012 17:48
Show Gist options
  • Save anonymous/3988629 to your computer and use it in GitHub Desktop.
Save anonymous/3988629 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.
class Counter
@@n = 0
@@counter = 0
def self.create_counter
@@counter+=1
if (@@counter%2==1)
proc { @@n+=1 }
else
proc { @@n-=1 }
end
end
end
first_counter = Counter.create_counter
puts "first_counter.call #{first_counter.call}"
puts "first_counter.call #{first_counter.call}"
second_counter = 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