diff --git a/src/ruby/lib/grpc/generic/service.rb b/src/ruby/lib/grpc/generic/service.rb index 93577bdd6be..86de6e8d027 100644 --- a/src/ruby/lib/grpc/generic/service.rb +++ b/src/ruby/lib/grpc/generic/service.rb @@ -31,6 +31,7 @@ module GRPC # # @param s [String] the string to be converted. def self.underscore(s) + s = +s # Avoid mutating the argument, as it might be frozen. s.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') s.gsub!(/([a-z\d])([A-Z])/, '\1_\2') s.tr!('-', '_') diff --git a/src/ruby/spec/generic/service_spec.rb b/src/ruby/spec/generic/service_spec.rb index 57aec89ce51..16f8783152e 100644 --- a/src/ruby/spec/generic/service_spec.rb +++ b/src/ruby/spec/generic/service_spec.rb @@ -55,6 +55,8 @@ describe GenericService do expect(GenericService.underscore('AMethod')).to eq('a_method') expect(GenericService.underscore('PrintHTML')).to eq('print_html') expect(GenericService.underscore('SeeHTMLBooks')).to eq('see_html_books') + + expect(GenericService.underscore('SeeHTMLBooks'.freeze)).to eq('see_html_books') end end