Wrap Your String Construction Into a Class Method
"Dotted format" is really a set of chained methods. While you could conceivably do something similar to what you're trying to do by appending each class method's output to a String in your method chain, I would consider this a brittle and unsavory OOP design.
On a semantic level, each method in a chain means "do x to the return value of the previous method." When you want to describe the members of an object, or a different kind of initialization, there are more idiomatic ways to do it.
Without making significant changes to your existing code, you can simply add a PhoneNumber#create class method to do the heavy lifting for you. For example:
module PhoneNumber def self.create [self.prefix, self.country, self.code, self.number].join end def self.prefix '+' end def self.country rand(1..9).to_s end def self.code rand(100..999).to_s end def self.number rand(1000000..9999999).to_s end end if __FILE__ == $0 puts PhoneNumber.create end
Joining the array of String objects returned by your other class methods is reasonably idiomatic, semantically clear, and sidesteps the need to alter the existing class methods, which other objects in your programs may currently rely on. This localizes change, which is often a good thing in OOP design.
Parameterizing a method or converting your module to a class, as described in other answers to your question, are also reasonable alternatives. Naturally, your mileage and stylistic tastes may vary.