aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector
Commit message (Collapse)AuthorAgeFilesLines
...
* revises a regexpXavier Noria2016-07-221-1/+1
| | | | The exclamation mark is not a metacharacter.
* systematic revision of =~ usage in ASXavier Noria2016-07-221-2/+3
| | | | | Where appropriate prefer the more concise Regexp#match?, String#include?, String#start_with?, and String#end_with?
* fix ActiveSupport::Infector.constantize usage API doc [ci skip]Pan GaoYong2016-06-301-4/+4
|
* Fix method String#upcase_firstbogdanvlviv2016-03-311-2/+4
|
* Add upcase_first methodGlauco Custódio2016-02-251-0/+7
|
* fix typo on commentsPaulo Ancheta2015-12-231-1/+1
|
* Parameterize with options to preserve case of stringSwaathi K2015-11-071-8/+23
| | | | | | | | | | | | | | | | | | | | | | | | Added test cases Using kwargs instead of three seperate functions Updated parameterize in transliterate.rb Updated parameterize in transliterate.rb Added deprecation warnings and updating RDoc+Guide Misspelled separtor. Fixed. Deprecated test cases and added support to parameterize with keyword parameters Squashing commits. Fixed test cases and added deprecated test cases Small changes to Gemfile.lock and CHANGELOG Update Gemfile.lock
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* code gardening in transliterate.rbXavier Noria2015-10-071-2/+5
| | | | | | | | | Saw this while doing a review of a patch: * Normalize case and punctuation across comments. * ascii -> ASCII * Since I was on it, some blank lines that visually add some clarity IMO.
* s/seperator/separator/gAkira Matsuda2015-09-201-3/+3
|
* Replaced `ThreadSafe::Map` with successor `Concurrent::Map`.Jerry D'Antonio2015-09-191-2/+2
| | | | | | | The thread_safe gem is being deprecated and all its code has been merged into the concurrent-ruby gem. The new class, Concurrent::Map, is exactly the same as its predecessor except for fixes to two bugs discovered during the merge.
* File encoding is defaulted to utf-8 in Ruby >= 2.1Akira Matsuda2015-09-182-3/+0
|
* Merge pull request #21217 from myrridin/myrridin-documentation-updatesZachary Scott2015-08-121-2/+2
|\ | | | | [ci skip] Documentation: Switch around a common phrase for readability
| * [ci skip] Switch around a common idiom for readabilityThomas Hart II2015-08-051-2/+2
| |
* | Regexp#=== is faster than String#=~schneems2015-08-071-1/+1
| | | | | | | | Discussion https://github.com/JuanitoFatas/fast-ruby/pull/59#issuecomment-128513763
* | speed up code and avoid unnecessary MatchData objectsAaron Lasseigne2015-08-061-1/+1
| |
* | Decrease allocations in transliterateschneems2015-07-301-5/+13
| | | | | | | | | | | | We can save a few objects by freezing the `replacement` string. We save a few more by down-casing the string in memory instead of allocating a new one. We save far more objects by checking for the default separator `"-"`, and using pre-generated regular expressions. We will save 209,231 bytes and 1,322 objects.
* | Decrease string allocations in apply_inflectionsschneems2015-07-292-5/+36
|/ | | | | | In `apply_inflections` a string is down cased and some whitespace stripped in the front (which allocate strings). This would normally be fine, however `uncountables` is a fairly small array (10 elements out of the box) and this method gets called a TON. Instead we can keep an array of valid regexes for each uncountable so we don't have to allocate new strings. This change buys us 325,106 bytes of memory and 3,251 fewer objects per request.
* Fix tests broken by previous commitSean Griffin2015-07-191-1/+1
|
* Freeze string literals when not mutated.schneems2015-07-192-11/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called [let_it_go](https://github.com/schneems/let_it_go). After going through the output and adding `.freeze` I was able to eliminate the creation of 1,114 string objects on EVERY request to [codetriage](codetriage.com). How does this impact execution? To look at memory: ```ruby require 'get_process_mem' mem = GetProcessMem.new GC.start GC.disable 1_114.times { " " } before = mem.mb after = mem.mb GC.enable puts "Diff: #{after - before} mb" ``` Creating 1,114 string objects results in `Diff: 0.03125 mb` of RAM allocated on every request. Or 1mb every 32 requests. To look at raw speed: ```ruby require 'benchmark/ips' number_of_objects_reduced = 1_114 Benchmark.ips do |x| x.report("freeze") { number_of_objects_reduced.times { " ".freeze } } x.report("no-freeze") { number_of_objects_reduced.times { " " } } end ``` We get the results ``` Calculating ------------------------------------- freeze 1.428k i/100ms no-freeze 609.000 i/100ms ------------------------------------------------- freeze 14.363k (± 8.5%) i/s - 71.400k no-freeze 6.084k (± 8.1%) i/s - 30.450k ``` Now we can do some maths: ```ruby ips = 6_226k # iterations / 1 second call_time_before = 1.0 / ips # seconds per iteration ips = 15_254 # iterations / 1 second call_time_after = 1.0 / ips # seconds per iteration diff = call_time_before - call_time_after number_of_objects_reduced * diff * 100 # => 0.4530373333993266 miliseconds saved per request ``` So we're shaving off 1 second of execution time for every 220 requests. Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep. p.s. If you know of a method call that doesn't modify a string input such as [String#gsub](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37) please [give me a pull request to the appropriate file](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37), or open an issue in LetItGo so we can track and freeze more strings. Keep those strings Frozen ![](https://www.dropbox.com/s/z4dj9fdsv213r4v/let-it-go.gif?dl=1)
* Use block variable instead of globalRoque Pinel2015-06-091-1/+1
|
* Use block variable instead of globalschneems2015-06-011-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby require 'benchmark/ips' Benchmark.ips do |x| x.report("$&") { "foo".sub(/f/) { $&.upcase } } x.report("block var") { "foo".sub(/f/) {|match| match.upcase } } end ``` ``` Calculating ------------------------------------- $& 48.658k i/100ms block var 49.666k i/100ms ------------------------------------------------- $& 873.156k (± 9.3%) i/s - 4.331M block var 969.744k (± 9.2%) i/s - 4.818M ``` It's faster, and gets rid of a few "magic" global variables
* [ci skip] remove unnecessary mention to Test::Unit from docsRoque Pinel2015-05-161-4/+4
| | | | | | | | Fix the guide to state that Rails uses Minitest as the default test framework. Remove unnecessary mention to Test::Unit from the API docs ('constantize' and 'safe_constantize').
* Freeze static arguments for gsubbrainopia2015-04-021-2/+2
|
* Prefer string patterns for gsubbrainopia2015-04-021-2/+2
| | | | | | | | | | | | | | | | | https://github.com/ruby/ruby/pull/579 - there is a new optimization since ruby 2.2 Previously regexp patterns were faster (since a string was converted to regexp underneath anyway). But now string patterns are faster and better reflect the purpose. Benchmark.ips do |bm| bm.report('regexp') { 'this is ::a random string'.gsub(/::/, '/') } bm.report('string') { 'this is ::a random string'.gsub('::', '/') } bm.compare! end # string: 753724.4 i/s # regexp: 501443.1 i/s - 1.50x slower
* Fix docs for ActiveSupport::Inflector methodsclaudiob2015-01-032-81/+71
| | | | | | | | | | | | | | | Many methods in ActiveSupport::Inflector were actually documenting the String methods with the same name. For instance the doc for `camelize` said: > If the argument to +camelize+ is set to <tt>:lower</tt>... while it should say: > If the +uppercase_first_letter+ parameter is set to false [ci skip]
* Call gsub with a Regexp instead of a String for better performancePablo Herrero2014-11-011-1/+1
|
* Do gsub with a regexp instead of a stringPablo Herrero2014-10-291-1/+1
|
* Reword documentation for `uncountable` [ci skip]Melanie Gilman2014-10-231-1/+1
|
* :heart: 1.9Matthew Draper2014-10-041-1/+1
|
* Merge pull request #14146 from chewi/fix-underscore-acronyms-regexMatthew Draper2014-10-041-1/+1
|\ | | | | | | Fix underscore inflector handling of namespaced and adjacent acronyms
| * Fix underscore inflector handling of adjacent acronymsJames Le Cuirot2014-10-031-1/+1
|/ | | | | | | | | | | | | I suspect that positive lookbehind would have been used in the original implementation had it been available in supported Ruby versions at the time. Now that Rails requires Ruby 1.9.2 or above, this is no longer an issue. This fixes #14146 for acronyms such as APIRESTful. This technique also addresses namespaced acronyms that are not entirely uppercased. This was broken when the commit was originally written but has since been fixed in ccbb481. The latter does not deal with adjacent acronyms so this commit wins.
* Fix for inflector's incorrect camelCase replacement for acronymsMatthew Draper2014-09-061-1/+1
| | | | | | Fixes #8015, #9756. [Fred Wu & Matthew Draper]
* Merge pull request #16338 from robin850/rbx-safe-constantizeMatthew Draper2014-08-181-2/+2
|\ | | | | Avoid relying on error messages when rescuing
| * Avoid relying on error messages when rescuingRobin Dupret2014-08-081-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When we are rescuing from an error, it's a brittle approach to do checks with regular expressions on the raised message because it may change in in the future and error messages are different across implementations. The NameError API could be improved at the MRI level but for now we need to rely on its #name. A #== check will only pass for top level constants or only when the last constant of the path is missing so we need to rely on #include? instead. For instance: begin Namespace::Foo rescue NameError => e e.name # => :Namespace end However, if the name-space already exists, only the name of the first missing constant in the path is returned (e.g. for Math::PHI, the name would be :PHI). JRuby will return a fully qualified name (:"Math::PHI"). We need to keep the == check for 1.9 compatibility since const_get will raise a NameError with a name attribute set to the given string if it's one of "::" or "". See http://git.io/jnSN7g for further information.
* | Fix the #const_regexp's documentation [ci skip]Robin Dupret2014-08-081-3/+4
|/ | | | | | Originally introduced in f9086d63, the documentation of this method is wrong as #const_regexp returns a string to easy the interpolation inside regular expressions.
* Make the apply_inflections method case-sensitiveRobin Dupret2014-06-261-1/+1
| | | | | | | | | | | | | | | | | | | Since d3071db1, the apply_inflections method check if the downcased version of a string is contained inside the "whitelist" of uncountable words. However, if the word is composed of capital letters, it won't be matched in the list while it should. We can't simply revert to the previous behavior as there is a performance concern (benchmarked over /usr/share/dict/words): Before d3071db1 135.610000 0.290000 135.900000 (137.807081) Since d3071db1 22.170000 0.020000 22.190000 ( 22.530005) With the patch 22.060000 0.020000 22.080000 ( 22.125771) Benchmarked with http://git.io/aFnWig This way, the solution is to put the down-case version of words inside the @uncountables array.
* several enhancements to humanize [closes #12288]Xavier Noria2014-05-061-9/+29
| | | | | | | | | | * Strips leading underscores. * Changes some unnecessary gsub!s to sub!s. * Replaces some anchors ^, $ with \A, \z. * Documents that human inflection rules are applied. * Documents that words are downcased except acronyms. * Adds an example with an acronym. * Rewords docs.
* Merge pull request #14840 from akshay-vishnoi/doc_changesArthur Nogueira Neves2014-04-231-2/+2
|\ | | | | Correct comment [ci skip]
| * Correct comment [ci skip]Akshay Vishnoi2014-04-231-2/+2
| |
* | Changed miss-leading comment [ci skip]Arun Agrawal2014-04-201-1/+1
| | | | | | | | | | | | | | | | | | | | | | After this 21dbe6f39b57f52967e92716dbd5e2b894e7a64c 2.1.1 :001 > 'business'.classify => "Business" 2.1.1 :004 > 'calculus'.classify => "Calculu" 2.1.1 :005 >
* | [ci-skip] correct the classify example's outputKuldeep Aggarwal2014-04-211-1/+1
|/
* [ci skip] builtin -> built-inAkshay Vishnoi2014-04-201-2/+2
|
* Add more test case for #demodulize, Improve documentationAkshay Vishnoi2014-04-111-0/+2
|
* speed up `underscore` in cases that don't need to do anythingAaron Patterson2014-02-261-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Benchmark: Benchmark.ips do |x| x.report("home") { "home".underscore } x.report("Home") { "Home".underscore } x.report("homeBase") { "homeBase".underscore } x.report("home::base") { "home::base".underscore } end Before: Calculating ------------------------------------- home 2598 i/100ms Home 2463 i/100ms homeBase 2300 i/100ms home::base 2086 i/100ms ------------------------------------------------- home 28522.3 (±14.7%) i/s - 140292 in 5.065102s Home 29165.8 (±14.9%) i/s - 140391 in 5.000475s homeBase 26218.5 (±7.1%) i/s - 131100 in 5.030485s home::base 27712.3 (±5.9%) i/s - 139762 in 5.064077s After: Calculating ------------------------------------- home 23163 i/100ms Home 2432 i/100ms homeBase 2160 i/100ms home::base 2265 i/100ms ------------------------------------------------- home 1501614.8 (±10.2%) i/s - 7412160 in 5.009540s Home 28754.0 (±8.5%) i/s - 143488 in 5.033886s homeBase 25331.1 (±5.6%) i/s - 127440 in 5.047940s home::base 27089.9 (±5.5%) i/s - 135900 in 5.033516s
* fix interplay of humanize and html_escapeSean Walbran2014-02-051-1/+1
|
* Replace with gsub! in ActiveSupport::Inflector.Elben Shira2013-12-101-1/+2
|
* Change syntax format for example returned valuesPrem Sichanugrist2013-11-111-17/+17
| | | | | | | | | According to our guideline, we leave 1 space between `#` and `=>`, so we want `# =>` instead of `#=>`. Thanks to @fxn for the suggestion. [ci skip]
* Add +capitalize+ option to Inflector.humanizeclaudiob2013-11-061-9/+15
| | | | | | | So strings can be humanized without being capitalized: 'employee_salary'.humanize # => "Employee salary" 'employee_salary'.humanize(capitalize: false) # => "employee salary"
* Fix doc for singularize - `pluralized` => `singularized`Vipul A M2013-08-271-1/+1
|