Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Possibly clearer way of getting rid of ` and " #4843

Merged
merged 2 commits into from

3 participants

@seamusabshere

hi,

You may find String#tr clearer than String#gsub in this case (YMMV)

Incidentally it's also faster...
>> a = 'hello "idworld'; Benchmark.realtime { 500_000.times { a.tr('"', "") } }
=> 0.7388770580291748
>> a = 'hello "idworld'; Benchmark.realtime { 500_000.times { a.gsub(/["]/, "") } }
=> 1.7843739986419678

Best,
Seamus

@seamusabshere seamusabshere Possibly clearer way of getting rid of ` and "
Incidentally it's also faster...
>> a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.tr('`"', "") } }
=> 0.7388770580291748 
>> a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.gsub(/[`"]/, "") } }
=> 1.7843739986419678
bbef7b0
@jurriaan

What about String#delete?

>>  a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.delete('`"') } }
=> 0.683949
>>  a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.tr('`"', "") } }
=> 0.782426
@seamusabshere

RAILS IS GONNA BE SO FAST :) I love it.

@jurriaan

Haha! I hope 4.0 will be a lot faster :)

@fxn
Owner

Better, thanks!

@fxn fxn merged commit bbd2cae into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 2, 2012
  1. @seamusabshere

    Possibly clearer way of getting rid of ` and "

    seamusabshere authored
    Incidentally it's also faster...
    >> a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.tr('`"', "") } }
    => 0.7388770580291748 
    >> a = 'hello "id` world'; Benchmark.realtime { 500_000.times { a.gsub(/[`"]/, "") } }
    => 1.7843739986419678
Commits on Feb 14, 2012
  1. @seamusabshere

    thanks to @jurriaan

    seamusabshere authored
This page is out of date. Refresh to see the latest.
View
2  activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -505,7 +505,7 @@ def pk_and_sequence_for(table)
execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result|
create_table = each_hash(result).first[:"Create Table"]
if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/
- keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") }
+ keys = $1.split(",").map { |key| key.delete('`"') }
keys.length == 1 ? [keys.first, nil] : nil
else
nil
Something went wrong with that request. Please try again.