Skip to content

Commit

Permalink
Merge pull request #1365 from herwinw/matchdata_captures
Browse files Browse the repository at this point in the history
Implement MatchData#deconstruct
  • Loading branch information
seven1m authored Oct 20, 2023
2 parents 5dccf24 + 6a67a76 commit 291be94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def generate_name
gen.undefine_singleton_method('MatchData', 'allocate')
gen.binding('MatchData', 'begin', 'MatchDataObject', 'begin', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('MatchData', 'byteoffset', 'MatchDataObject', 'byteoffset', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('MatchData', 'captures', 'MatchDataObject', 'captures', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('MatchData', 'captures', 'MatchDataObject', 'captures', argc: 0, pass_env: true, pass_block: false, aliases: ['deconstruct'], return_type: :Object)
gen.binding('MatchData', 'end', 'MatchDataObject', 'end', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('MatchData', 'inspect', 'MatchDataObject', 'inspect', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('MatchData', 'match', 'MatchDataObject', 'match', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
Expand Down
11 changes: 2 additions & 9 deletions spec/core/matchdata/captures_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/captures'

describe "MatchData#captures" do
it "returns an array of the match captures" do
/(.)(.)(\d+)(\d)/.match("THX1138.").captures.should == ["H","X","113","8"]
end

it "returns instances of String when given a String subclass" do
str = MatchDataSpecs::MyString.new("THX1138: The Movie")
/(.)(.)(\d+)(\d)/.match(str).captures.each { |c| c.should be_an_instance_of(String) }
end
it_behaves_like :matchdata_captures, :captures
end
8 changes: 8 additions & 0 deletions spec/core/matchdata/deconstruct_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../../spec_helper'
require_relative 'shared/captures'

describe "MatchData#deconstruct" do
ruby_version_is "3.2" do
it_behaves_like :matchdata_captures, :deconstruct
end
end
13 changes: 13 additions & 0 deletions spec/core/matchdata/shared/captures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'

describe :matchdata_captures, shared: true do
it "returns an array of the match captures" do
/(.)(.)(\d+)(\d)/.match("THX1138.").send(@method).should == ["H","X","113","8"]
end

it "returns instances of String when given a String subclass" do
str = MatchDataSpecs::MyString.new("THX1138: The Movie")
/(.)(.)(\d+)(\d)/.match(str).send(@method).each { |c| c.should be_an_instance_of(String) }
end
end

0 comments on commit 291be94

Please sign in to comment.