Created
May 8, 2014 18:40
-
-
Save forest/bf686082947434b36584 to your computer and use it in GitHub Desktop.
Sugar-Free Ruby: An Experiment in Object-first Teaching
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
# http://www.confreaks.com/videos/1112-gogaruco2012-sugar-free-ruby-an-experiment-in-object-first-teaching | |
# https://github.com/simeonwillbanks/gogaruco2012/blob/master/02_Sugar-Free_Ruby:_An_Experiment_in_Object-first_Teaching.md | |
# Everything is an Object. | |
5 # => 5 | |
42.0 # => 42.0 | |
"Hello!" # => Hello! | |
[1, 2, 3] # => [1, 2, 3] | |
# Objects receive messages. | |
"six".send :upcase # =>SIX | |
5.send :+, 9 # => 14 | |
17.send :nil? # => false | |
# Objects have a Class. | |
5.send :class # => Fixnum | |
4.0.send :class # => Float | |
["he's", "crazy"].send :class # => Array | |
# Teaching Objects | |
# | |
# The pen and the scribe. | |
# 1. Create a new Object and give it a name. | |
pen = Object.new | |
# => #<Object:0x007faae3882238> | |
# 2. Define a method. Methods respond to messages. | |
# Give the pen a nicer name than above. | |
def pen.to_s | |
"A trusty pen" | |
end | |
pen.send :to_s | |
# => A trusty pen | |
# 3. A Pinch of sugar. | |
pen.to_s | |
# => A trusty pen | |
# 4. Errors are your friend. | |
# They tell you what to do next. | |
pen.write "Hi there" | |
# => NoMethodError: undefined method `write' for #<Object:A trusty pen> | |
# 5. Define the missing method | |
# Write all the things. | |
def pen.write thing | |
Kernel.puts thing | |
end | |
pen.write "Hi" | |
# => Hi | |
pen.write pen | |
# => A trusty pen | |
# 6. Feature Request: Color | |
# We want the pen to contain an ink color. | |
pen.ink_color = "Blue" | |
# => NoMethodError: undefined method `ink_color=' for #<Object:A trusty pen> | |
# 7. Use an instance variable to save the ink color. | |
def pen.ink_color= color | |
@ink_color = color | |
end | |
# 8. Show our colors. | |
# Redefine the to_s method to show the pen's color. | |
def pen.to_s | |
"A trusty #{@ink_color} pen" | |
end | |
pen.ink_color = "purple" | |
pen.to_s | |
# => A trusty purple pen | |
# 9. Make it fancy. | |
# Implicit Receivers: | |
# If you don't specify the Object to send a message to | |
# it sends the message to itself. | |
def pen.sparkly_write thing | |
write "*;. #{thing} *;.*" | |
end | |
pen.sparkly_write pen | |
# => *;. A trusty purple pen *;.* | |
# 10. Conditionals | |
# Redefine write to show the ink color if it is set. | |
# http://ascii-table.com/ansi-escape-sequences.php | |
# 30 Black | |
# 31 Red | |
# 32 Green | |
# 33 Yellow | |
# 34 Blue | |
# 35 Magenta | |
# 36 Cyan | |
# 37 White | |
def pen.write thing | |
if @ink_color.downcase == 'black' | |
Kernel.puts "\033[0;30;40m#{thing}\033[m" | |
elsif @ink_color.downcase == 'red' | |
Kernel.puts "\033[0;31;40m#{thing}\033[m" | |
elsif @ink_color.downcase == 'green' | |
Kernel.puts "\033[0;32;40m#{thing}\033[m" | |
elsif @ink_color.downcase == 'yellow' | |
Kernel.puts "\033[0;33;40m#{thing}\033[m" | |
elsif @ink_color.downcase == 'blue' | |
Kernel.puts "\033[0;34;40m#{thing}\033[m" | |
elsif @ink_color.downcase == 'purple' | |
Kernel.puts "\033[0;35;40m#{thing}\033[m" | |
else | |
Kernel.puts thing | |
end | |
end | |
pen.write "I see #{pen.ink_color}" | |
# => I see purple | |
# 11. Define a Class. | |
# Class is a class that creates new classes. | |
Scribe = Class.new do | |
def initialize pen | |
@pen = pen | |
@items = Array.new | |
end | |
def note item | |
@items.push item | |
end | |
def write_everything | |
@items.each do |item| | |
@pen.write item | |
end | |
end | |
end | |
# 12. Puttig the Scribe to work. | |
fred = Scribe.new pen | |
# => #<Scribe:0x007f9df1a943e0 @items=[], @pen=#<Object:0x007f9df9a84730 @ink_color="green">> | |
fred.note "Time: #{Time.now}" | |
fred.note "Programming is awesome!" | |
fred.write_everything | |
# => Time: 2014-04-30 00:18:39 -0700 | |
# => Programming is awesome! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment