Refactoring - Extract Class

This topic is for the [Extract Class] exercise in the [Refactoring] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Extract Class]:
[Refactoring]: Refactoring Code | Code Refactoring Online Tutorial by thoughtbot

When I run rake on the Extract Class program, before any changes, I get an error:

/Users/louiserains/.rbenv/versions/2.2.1/bin/ruby -I/Users/louiserains/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.1/lib:/Users/louiserains/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rspec-support-3.2.2/lib /Users/louiserains/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.1/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/Users/louiserains/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/number_helper.rb:3:in `<module:NumberHelper>': uninitialized constant ActiveSupport::Autoload (NameError)

Any ideas?

I solved the problem by adding the following in spec_helper.rb:

require 'active_support'
require 'active_support/core_ext'

based on this: uninitialized constant ActiveSupport::Autoload (NameError), sometimes, at ramdom · Issue #14664 · rails/rails · GitHub


I also got this. Should a change be made to the initial code on Thoughtbot’s side? I needed to add:

require 'active_support'

I’m using Ruby 2.2.2. I can run it just by using bundle exec rspec.

1 Like

Maybe I’m missing something but I want to ask question regarding this exercise.

@recipients.gsub(/\s+/, '').split(/[\n,;]+/)

The above code was supposed to strip spaces and split the emails. But I don’t think it’s working with emails with \n as /\s+/ will always strip it first. So, emails with new lines wont’ work here. For e.g:

irb(main):002:0> emails = "\"
=> "\"
irb(main):003:0> emails.gsub(/\s+/, '').split(/[\n,;]+/)
=> [""]

I think we should split it first then we can strip the spaces. Even the featured solution doesn’t test emails with \n

Thanks in advance for the feedback.

Great catch! Here’s an updated version incorporating your changes, how does this one look?


Nice, definitely better :+1:

After contemplating about the code a couple of minutes I still don’t quite understand the task. Why should it be necessary to factor out a more-or-less well-readable one-liner (recipient_list) into its own class? Shouldn’t then be more functionality be delegated to that class?

Just to let you know that this code breaks in Ruby 2.4.x and I had to use Ruby 2.3.x.

1 Like