Greetings from Ruby Code Camp in Columbus, OH and thanks to the Columbus Ruby Brigade for spreading the word and to all the sponsors, especially Agile Consulting and EdgeCase. Here’s what I’ve learned…
Beside many other brilliant things he said, Rob Stevenson demonstrated the
following (figurative – not literal) gem that will display all methods to which an
object will respond:
>> 1.methods.each { |i| printf "Method name: %s\n", i.to_s }
Method name: %
Method name: inspect
Method name: <<
Method name: singleton_method_added
Method name: &
Method name: clone
Method name: >>
Method name: method
Method name: round
Method name: rpower
Method name: public_methods
...
As Rob Biedenharn was setting up for the next session he had the contents
of his .irbrc file which among other things contained the following:
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT] = true
require 'rubygems'
def clear
system "clear"
end
alias :cls :clear
Setting the irb prompt to simple removes everything but the two
“greater than” characters. The auto indent feature indents your
input automatically (duh!) according to Ruby code conventions which is
something Jim Weirich apparently didn’t know and although I would love to be
able to tell folks that I taught Jim something about Ruby i only learned it
by googling it two minutes prior to showing Jim. Since I use rubygems a lot
I add those in by default, and since I like to keep my screen clean so I
stole, eh…, I mean re-used Rob’s “clean” method setting an alias for called
“cls”. I combined this knowledge to create the following means to output a
sorted list of all methods to which a given object will respond:
def pretty_methods obj
obj.methods.sort.each { |i| printf "%s\n", i.to_s }
end
alias :pm :pretty_methods
During the second hour of Ruby Introduction taught by Ron McCamish and coded by Rob Biedenharn I learned a new thing or three as the case may be about quickly getting at regular expression match data:
irb
>> b = /v(olly)?ball/
=> /v(olly)?ball/
>> str = "My vollyball is Tachikara"
=> "My vollyball is Tachikara"
>> str =~ b
=> 3
>> $`
=> "My "
>> $&
=> "vollyball"
>> $'
=> " is Tachikara"
Finally, I learned that I need to better understand closures, the difference between “detect” and “select”, and that I will never be able to code as well as Jim Weirich.