Notes posted by canadaduane

RSS feed
September 5, 2008
3 thanks

Require 'strscan'

To use the StringScanner class,

require 'strscan'
August 16, 2008
1 thank

Examples of Setting the Prefix and Suffix

Both of the following will work for setting the prefix or suffix:

class Mouse < ActiveRecord::Base
  self.table_name_prefix = 'forum_'


class Mouse < ActiveRecord::Base
  def self.table_name_prefix
August 14, 2008 - (v1_8_6_287)
1 thank
August 14, 2008 - (v1_8_6_287)
11 thanks

Convert an Array to a Hash

The Hash.[] method converts an even number of parameters to a Hash. (The Hash[] method depends on the Hash class, but don’t confuse the method with the class itself). For example:

Hash['A', 'a', 'B', 'b']
# => {"A"=>"a", "B"=>"b"}

You can convert an array to a hash using the Hash[] method:

array = ['A', 'a', 'B', 'b', 'C', 'c']
hash = Hash[*array]
# => {"A"=>"a", "B"=>"b", "C"=>"c"}  

The * (splat) operator converts the array into an argument list, as expected by Hash[].

You can similarly convert an array of arrays to a Hash, by adding flatten:

array = [['A', 'a'], ['B', 'b'], ['C', 'c']]
hash = Hash[*array.flatten]  
# => {"A"=>"a", "B"=>"b", "C"=>"c"}

This also comes in handy when you have a list of words that you want to convert to a Hash:

  A a
  B b
  C c
# => {"A"=>"a", "B"=>"b", "C"=>"c"}