Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created November 9, 2010 21:06
Show Gist options
  • Save jrochkind/669810 to your computer and use it in GitHub Desktop.
Save jrochkind/669810 to your computer and use it in GitHub Desktop.
require_dependency 'vendor/plugins/blacklight/app/controllers/catalog_controller.rb'
class CatalogController < ApplicationController
# prepend_before_filter :transform_syntax
def solr_search_params(extra_controller_params={})
solr_parameters = super extra_controller_params
my_parameters = solr_parameters.clone
if my_parameters.key? :q
my_parameters[:q] = my_parameters[:q].gsub(/\S+/) { |match|
if match.include? ':' or match.index('=') != 0
match
else
match.sub("=", "#{Blacklight.config[:nosyn_field]}:")
end
}
end
return my_parameters
end
end
~
@jrochkind
Copy link
Author

irb(main):006:0> a = "foo"
=> "foo"
irb(main):007:0> b = a
=> "foo"
irb(main):008:0> a.gsub!(/foo/, "New one!")
=> "New one!"
irb(main):009:0> a
=> "New one!"
irb(main):010:0> b
=> "New one!"
irb(main):014:0> a === b
=> true

gsub! actually changes the string object itself, both 'a' and 'b' point to the same string object, mutating it means it's different for both, because there's only one string there, and you've changed it. vs:

irb(main):011:0> x = "foo"
=> "foo"
irb(main):012:0> y = x
=> "foo"
irb(main):013:0> x = x.gsub(/foo/, "New one!")
=> "New one!"
irb(main):016:0> y
=> "foo"

x and y no longer point to the same string object, because instead of mutating that obj with gsub!, you created a new string based on the old with gsub, and assigned it to x.

irb(main):017:0> x === y
=> false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment