← Back to Upcase

Refactoring - Extract Class


(Upcase ) #1

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]: https://exercises.upcase.com/exercises/extract-class
[Refactoring]: https://thoughtbot.com/upcase/refactoring


(Louise Rains) #2

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

rake
/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?


(Louise Rains) #3

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

require 'active_support'
require 'active_support/core_ext'

based on this: https://github.com/rails/rails/issues/14664


(Andrew Charles Potter Kelley) #5

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

require 'active_support'

(Amree Zaid) #6

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


(Amree Zaid) #7

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 = "one@example.com\ntwo@example.com"
=> "one@example.com\ntwo@example.com"
irb(main):003:0> emails.gsub(/\s+/, '').split(/[\n,;]+/)
=> ["one@example.comtwo@example.com"]

I think we should split it first then we can strip the spaces. Even the featured solution doesn’t test emails with \n https://exercises.upcase.com/exercises/extract-class/solutions/trevor

Thanks in advance for the feedback.


(Raghu Betina) #8

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

Thanks!


(Amree Zaid) #9

Nice, definitely better :+1:


(Andreas Neumann) #10

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?


(Mazin Hakeem) #12

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