Misunderstanding "scan"?

I could use some help with "scan", since I seem to be doing something wrong.

## Text used for test

## Failing test

## Method that builds the hash:

## From:

## Running the test
$ rspec spec/section_spec.rb -e contractions

Run options: include {:full_description=>/contractions/}
apos 16
pos 0
F

Failures:

   1) A section should count contractions
      Failure/Error: expect(section.contraction_count).to be(15)

        expected #<Integer:31> => 15
             got #<Integer:3> => 1

        Compared using equal?, which compares object identity,
        but expected and actual are not the same object. Use
        `expect(actual).to eq(expected)` if you don't care about
        object identity in this example.
      # ./spec/section_spec.rb:11:in `block (2 levels) in <module:BookBot>'

Finished in 0.01556 seconds (files took 0.12718 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/section_spec.rb:10 # A section should count contractions

Hi Leam,

I don't think your challenge is with `scan`, but with the Tools.scrub
method which modifies the string argument using `gsub!`. The Section
initializer will pass the string variable to Tools.build_report which in
turn passes it to Tools.scrub which modifies it. To see the effect of this,
print the @string in the Section initializer before and after you call
build_report.

A more idiomatic pattern would be to have utility methods treat string
arguments as immutable, preferring instead to return a modified copy of the
string. Hope that makes sense.

···

On Wed, Aug 5, 2020 at 7:36 AM Leam Hall <leamhall@gmail.com> wrote:

I could use some help with "scan", since I seem to be doing something
wrong.

## Text used for test

https://github.com/LeamHall/bookbot/blob/book_obj/spec/data/test_section.txt

## Failing test

https://github.com/LeamHall/bookbot/blob/book_obj/spec/section_spec.rb#L10-L12

## Method that builds the hash:

https://github.com/LeamHall/bookbot/blob/book_obj/lib/bookbot/section.rb#L85-L89

## From:

https://github.com/LeamHall/bookbot/blob/book_obj/lib/bookbot/tools.rb#L87-L99

## Running the test
$ rspec spec/section_spec.rb -e contractions

Run options: include {:full_description=>/contractions/}
apos 16
pos 0
F

Failures:

   1) A section should count contractions
      Failure/Error: expect(section.contraction_count).to be(15)

        expected #<Integer:31> => 15
             got #<Integer:3> => 1

        Compared using equal?, which compares object identity,
        but expected and actual are not the same object. Use
        `expect(actual).to eq(expected)` if you don't care about
        object identity in this example.
      # ./spec/section_spec.rb:11:in `block (2 levels) in <module:BookBot>'

Finished in 0.01556 seconds (files took 0.12718 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/section_spec.rb:10 # A section should count contractions

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Michael, thank you!

I think that's going to work. The first test passed. More to thing through though, and ensure I make the code simpler and more robust.

···

On 8/5/20 7:58 AM, Mike Dalessio wrote:

Hi Leam,

I don't think your challenge is with `scan`, but with the Tools.scrub
method which modifies the string argument using `gsub!`. The Section
initializer will pass the string variable to Tools.build_report which in
turn passes it to Tools.scrub which modifies it. To see the effect of this,
print the @string in the Section initializer before and after you call
build_report.

A more idiomatic pattern would be to have utility methods treat string
arguments as immutable, preferring instead to return a modified copy of the
string. Hope that makes sense.

On Wed, Aug 5, 2020 at 7:36 AM Leam Hall <leamhall@gmail.com> wrote:

I could use some help with "scan", since I seem to be doing something
wrong.

## Text used for test

https://github.com/LeamHall/bookbot/blob/book_obj/spec/data/test_section.txt

## Failing test

https://github.com/LeamHall/bookbot/blob/book_obj/spec/section_spec.rb#L10-L12

## Method that builds the hash:

https://github.com/LeamHall/bookbot/blob/book_obj/lib/bookbot/section.rb#L85-L89

## From:

https://github.com/LeamHall/bookbot/blob/book_obj/lib/bookbot/tools.rb#L87-L99

## Running the test
$ rspec spec/section_spec.rb -e contractions

Run options: include {:full_description=>/contractions/}
apos 16
pos 0
F

Failures:

    1) A section should count contractions
       Failure/Error: expect(section.contraction_count).to be(15)

         expected #<Integer:31> => 15
              got #<Integer:3> => 1

         Compared using equal?, which compares object identity,
         but expected and actual are not the same object. Use
         `expect(actual).to eq(expected)` if you don't care about
         object identity in this example.
       # ./spec/section_spec.rb:11:in `block (2 levels) in <module:BookBot>'

Finished in 0.01556 seconds (files took 0.12718 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/section_spec.rb:10 # A section should count contractions

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>