ara.t.howard / noaa.gov wrote:
> On Sun, 20 Aug 2006, Daniel Schierbeck wrote:
> 
>> Joshua Muheim wrote:
>>> Hi all
>>>
>>> What's the fastest way to convert "ThisIsSomeString" to 
>>> "this_is_some_string"?
>>
>> I believe this is the fastest way, though it may not catch all cases 
>> (numbers and such)
>>
>>  def lowerize(str)  # ??
>>    str.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
>>  end
>>
>>
>> Cheers,
>> Daniel
>>
> 
> depending on what you want, the snake_case code i posted works differently:
> 
>   irb(main):025:0> lowerize 'BARFoo'
>   => "barfoo"
> 
>   irb(main):026:0> snake_case 'BARFoo'
>   => "bar_foo"
> 
> neither is right - just fyi.

Correct. Your solution has a flaw, too.

   "fooBar".scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join('_') => "bar"

Lets see if we can solve this without peeking at the Rails source :)


Cheers,
Daniel