The columnize method removes whitespace
from the string, lowercases it, and replaces everything that is not a letter, a number,
or an underscore with an underscore. In most languages, this would be implemented as a
method that takes a string as an argument, but in Ruby, it??™s customary to simply extend
an existing class. (Of course, in most languages, you cannot extend an existing class by
adding methods to it, so you don??™t get a choice.) For example, you could use the columnize
method like this:
puts "A column name".columnize
And get the following result:
a_column_name
In other words, columnize converts strings into names that can easily be used as
MySQL column names.
nNote Active Support, which is part of Rails, also has a method named columnize, which works similarly
to the one discussed here. So, you wouldn??™t need to define this method if you were inside a Rails application.
See http://wiki.rubyonrails.org/rails/pages/ActiveSupport for more information. (You could
include Active Support in a script like this, but it??™s a large library, so using it here would be overkill.
Pages:
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209