# Use SRP to refactor this Ruby Class

I have a class, and it does one thing, It adds numerals to the numerals array.

In doing so it uses a number of private methods and has ended up looking a little bit sickening...

brace yourself...

require './lib/number_interpreter'

# A class for adding individual numerals
:num_interpreter,
:numeral

def initialize(num_to_convert)
@num_to_convert = num_to_convert
@num_interpreter = NumberInterpreter.new(num_to_convert)
@numeral = []
end

flatten_numeral
end

private

def flatten_numeral
@numeral = @numeral.flatten
end

end

end

end

end

end

end

end

end

end

end

end

end

end
end


I'm thinking I need to separate the private class creations into their own class, but i'm not sure how.

Any tips?

## migrated from stackoverflow.comSep 14 '16 at 19:40

This question came from our site for professional and enthusiast programmers.

• In situations like this you really want to use arrays of objects you call methods on and things like Dir.glob to auto-load in everything in a directory. What does one of these add_numerals methods look like? – tadman Sep 14 '16 at 19:36
• The Single Responsibility Principle is probably irrelevant to improving this code. What do the various adders in the included files look like? What is this code supposed to accomplish? Can you give an example of how it is to be used? Explain the purpose of the code, and make that the title of the question. See How to Ask. – 200_success Sep 16 '16 at 14:01

To start out with all of the

require './lib/numeral_adders/<lib>


should be moved to a separate file like/lib/number_interpreter

Moving on, looking at repetitive parts of your code. The add_numerals method has a lot of lines like this:

@numeral << <type>_adder.add_numerals.split('')


and you have methods like this:

def <type>_adder
end


You can make this DRY by enumerating over a list defined elsewhere. For example:

# in the lib/number_interpreter.rb file
class NumberInterpreter
# ...
}
end

# in the main file (numeral_adder.rb)

end
flatten_numeral
end


Important concepts here include setting a class as a hash key then initializing it using new later on. It also uses send for programmatic method invocation. You're writing a generic adder class to make all that repetition unnecessary - its arguments are a class to initialize and a unit to convert the results with.
By the way your flatten_numeral is so simple that it'd be better to not make it a separate method. The flatten would probably be unnecessary if you use concat instead of << (shovel):
@numeral.concat adder(klass, unit).add_numerals.split('')