I have some Ruby code that does the following:
type = Foo
limit = "num_allowed_#{type.downcase!}"
It's fairly straightforward code to tell me how many allowed widgets a given customer has, or frobbles ("limit" is the attribute that is the max allowed widgets or frobbles or whatever). There's some surprising behavior, though.
If the string I'm attempting to downcase is already entirely lowercase, then it becomes nil.
For example:
penitentes:src cpowell$ irb>> type = "Foo"=> "Foo">> quantity = "num_#{type.downcase!}"=> "num_foo">>?>?> type = "foo"=> "foo">> quantity = "num_#{type.downcase!}"=> "num_">>
That's not what I expected. If you don't use the bang (just use "downcase"), then it works as expected. For example:
penitentes:src cpowell$ irb>> type = "Foo"=> "Foo">> quantity = "num_#{type.downcase}"=> "num_foo">> type = "foo"=> "foo">> quantity = "num_#{type.downcase}"=> "num_foo">>
I'm not sure it's a bug, strictly, but it's certainly odd.
P.S. I've been coding a lot lately, so the posts are getting a bit technical. The pendulum will swing the other way at some point.
2 comments: