Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with frozen-string-literal and newer rubies #80

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Ruby

on: ['push']

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', 'head']
rubyopt: ['']
include:
- ruby: '3.3'
rubyopt: "--enable-frozen-string-literal --debug-frozen-string-literal"

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake RUBYOPT="${{ matrix.rubyopt }}"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gemfile.lock

4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
source 'https://rubygems.org'

gemspec

gem "rake"
gem "rspec"
6 changes: 3 additions & 3 deletions lib/method_source/code_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def complete_expression?(str)
# @return [String] a valid ruby expression
# @raise [SyntaxError]
def extract_first_expression(lines, consume=0, &block)
code = consume.zero? ? "" : lines.slice!(0..(consume - 1)).join
code = consume.zero? ? +"" : lines.slice!(0..(consume - 1)).join

lines.each do |v|
code << v
Expand All @@ -104,15 +104,15 @@ def extract_first_expression(lines, consume=0, &block)
# @param [Array<String>] lines
# @return [String]
def extract_last_comment(lines)
buffer = ""
buffer = +""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as String.new("")?

In case it is, maybe we could use it instead? It's less cryptic IMO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not quite the same. +@ only dup the string if it's not already mutable. String.new("") will always create a copy.

But it doesn't make a big difference, so I don't mind changing it. But then "".dup might be even more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided +"" is valid Ruby going back several versions (which I think it is - and the CI runs should confirm that), then I personally would prefer keeping it. Yes, right now it's not common syntax, but taking a step towards normalising this in Ruby code feels smart to me - it creates a single mutable string, which is exactly what's required.

String.new("") and "".dup are, as @casperisfine has noted, both duplicating strings - yes, it's a small thing, but I feel like it's not the best habit to lean into from a performance perspective.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for the clarification. This is what I found:

https://github.com/fastruby/fast-ruby?tab=readme-ov-file#string

String#dup vs String#+ code

Note that String.new is not the same as the options compared, since it is always ASCII-8BIT encoded instead of the script encoding (usually UTF-8).

$ ruby -v code/string/dup-vs-unary-plus.rb
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin17]

Calculating -------------------------------------
String#+@ 7.697M (± 1.4%) i/s - 38.634M in 5.020313s
String#dup 3.566M (± 1.0%) i/s - 17.860M in 5.008377s

Comparison:
String#+@: 7697108.3 i/s
String#dup: 3566485.7 i/s - 2.16x slower

Let's stick with +""


lines.each do |line|
# Add any line that is a valid ruby comment,
# but clear as soon as we hit a non comment line.
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
buffer << line.lstrip
else
buffer.replace("")
buffer.clear
end
end

Expand Down
15 changes: 0 additions & 15 deletions method_source.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,4 @@ Gem::Specification.new do |s|
s.licenses = ["MIT".freeze]
s.summary = "retrieve the sourcecode for a method".freeze
s.test_files = ["spec/method_source/code_helpers_spec.rb".freeze, "spec/method_source_spec.rb".freeze, "spec/spec_helper.rb".freeze]

if s.respond_to? :specification_version then
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>.freeze, ["~> 3.6"])
s.add_development_dependency(%q<rake>.freeze, ["~> 0.9"])
else
s.add_dependency(%q<rspec>.freeze, ["~> 3.6"])
s.add_dependency(%q<rake>.freeze, ["~> 0.9"])
end
else
s.add_dependency(%q<rspec>.freeze, ["~> 3.6"])
s.add_dependency(%q<rake>.freeze, ["~> 0.9"])
end
end