Skip to content

Commit

Permalink
Support for frozen string literals
Browse files Browse the repository at this point in the history
When run with the --frozen_string_literal flag, Ruby will throw an error
if you try to modify a string object.

Leaving discussions of whether this approach is idiomatic Ruby (or not)
to one side, it seems sensible in light of the discussion in [1] and [2]
for projects like Nesta to work without mutating strings. And note that
in Ruby 3.4, things are changing.

To find the areas of the code that warranted an update I ran the tests
under Ruby 3.3, like this:

    RUBYOPT=--enable=frozen-string-literal rake test

[1] https://bugs.ruby-lang.org/issues/16047
[2] https://bugs.ruby-lang.org/issues/20205
  • Loading branch information
gma committed Oct 5, 2024
1 parent 54eb8e1 commit d4b388c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/nesta/commands/plugin/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def nested_module_definition_with_version
indent_levels << indent_level
end

''.tap do |code|
lines.each_with_index do |line, i|
code << ' ' * (indent_levels[i] + 2) + line
end
code = []
lines.each_with_index do |line, i|
code << ' ' * (indent_levels[i] + 2) + line
end
code.join("\n")
end

def make_directories
Expand Down
4 changes: 2 additions & 2 deletions lib/nesta/config_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def set_value(key, value)

configured = false
File.open(self.class.path, 'r+') do |file|
output = ''
output = []
file.each_line do |line|
if configured
output << line
Expand All @@ -21,7 +21,7 @@ def set_value(key, value)
end
output << "#{replacement}\n" unless configured
file.pos = 0
file.print(output)
file.print(output.join("\n"))
file.truncate(file.pos)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/nesta/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def articles_heading
# path_to(page.abspath, uri: true)
#
def path_to(page_path, options = {})
host = ''
host = []
if options[:uri]
host << "http#{'s' if request.ssl?}://"
if (request.env.include?("HTTP_X_FORWARDED_HOST") or
Expand All @@ -100,7 +100,7 @@ def path_to(page_path, options = {})
host << request.host
end
end
uri_parts = [host]
uri_parts = [host.join('')]
uri_parts << request.script_name.to_s if request.script_name
uri_parts << page_path
File.join(uri_parts)
Expand Down
2 changes: 1 addition & 1 deletion lib/nesta/models/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.top_level
end

def self.for_path(path)
path.sub!(Regexp.new('^/'), '')
path = path.sub(Regexp.new('^/'), '')
if path.empty?
full_menu
else
Expand Down

0 comments on commit d4b388c

Please sign in to comment.