SyntaxHighlighter
June 3, 2008 2:14 am Education, Programming, Web DevelopmentI finally got a syntax highlighter installed. I figure that since I’m taking the free Ruby course at RubyLearning.org, I should make my code look nice (and be more usable) when I decide to put some here on my blog. Just to get things rolling, here is a bit of code from one of the exercises I’ve done during the first few weeks of the course.
class Dog
def initialize(name)
@name = name.capitalize
@what = self.class.to_s.downcase
puts "Hi! I'm #{@name}, your new #{@what}!"
end
def teach(new_trick)
@tricks = Array.new unless @tricks
@tricks += [new_trick]
puts "#{@name} now knows how to #{new_trick}!"
end
def show_tricks
list_of_tricks = [@tricks[0]]
1.upto(@tricks.length-1) do |modify|
list_of_tricks += [" and #{@tricks[modify]}"]
end
@name + ' knows how to ' + list_of_tricks.to_s
end
def bark
end
def eat
end
def chase_cat
end
def to_s
@name + " (your #{@what})"
end
def capable_of?(capability)
self.respond_to?(capability) ?
puts("I can #{capability} or my name's not #{@name}!") :
@tricks.include?(capability) ?
puts("I have recently been taught to #{capability}!") :
puts("This is something I have not heard of before... \
#{capability} you said?")
end
def method_missing(method, *more_arguments)
"#{@name} cocks it's head to the side as if to say \n\t\
\"I don't understand what #{method} means.\""
end
end
# a few examples of usage:
rex = Dog.new 'rex'
max = Dog.new 'Max'
rex.teach 'jump through hoops'
ObjectSpace.each_object(Dog) {|dog| dog.teach('chase cars') }
puts rex.show_tricks
rex.capable_of? 'bark'
rex.capable_of? 'chase cars'
rex.capable_of? 'fetch'
puts "#{max} is feeling a bit left out..."
Sadly, I had misinterpreted the challenge guidelines. The instructor wanted the learned tricks to be methods and not properties. Anyhoot, this post is just so I can see what the highlighter will do on my blog. :-P
