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]: Refactoring Code | Code Refactoring Online Tutorial by thoughtbot
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?
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
.
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.
Great catch! Hereâs an updated version incorporating your changes, how does this one look?
Thanks!
Nice, definitely better
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.