Codecast: Create a RubyGem 20: Change bonus option to use duck-typing


This is the 20th video in the NerdDice create a RubyGem series. In this video, Mike makes life a little easier on the future users of the gem by using Ruby’s powerful “duck-typing” (if it looks like a duck, and it quacks like a duck…) feature for the :total_dice bonus option. Instead of checking to see if the value passed is a kind of Integer, we check to see if it responds to the :to_i method. This allows String, Float, or other types that can be converted to an Integer to be passed as values for the bonus option without making the user do the work.

, ,

Leave a Reply