Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method by method in Ruby

Tags:

module

ruby

I have a module that generates a phone number in the format I need.

module PhoneNumber
  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

I use it as follows. Or as a formatted string "#{}#{}".

phone_number = PhoneNumber.prefix +
               PhoneNumber.country +
               PhoneNumber.code +
               PhoneNumber.number

I want to rewrite the body of the module in this way, so that I can use it in a dotted format.

PhoneNumber.prefix.code.number
like image 253
Sergey Blohin Avatar asked Jun 08 '26 23:06

Sergey Blohin


2 Answers

I agree with Todd that you shouldn't do this; it will be awkward to implement and confusing for people who read the code (including yourself in the future) as it's not the typical way to write the sort of operations you're talking about.

That said, if you really want to do so, you'll need to provide a method called prefix which returns another object which has a method code and so on, storing state along the chain and combining the strings at the end. It would get even more complicated if you want to swap around the order.

Strongly recommend a method that looks like this instead:

PhoneNumber.build(:prefix, :code, :number)
like image 95
elliotcm Avatar answered Jun 11 '26 17:06

elliotcm


To chain methods one should basically constantly return self from all the methods you are going to chain:

module PhoneNumber
  @number = ''
  def self.prefix
    @number << '+'
    self
  end
  def self.country
    @number << rand(1..9).to_s
    self
  end
  def self.code
    @number << rand(100..999).to_s
    self
  end
  def self.number
    @number << rand(1000000..9999999).to_s
    self
  end
  def self.to_s
    @number
  end
end

puts PhoneNumber.prefix.code.number
#⇒ +9065560843

Note the explicit #to_s implementation for the last step, since you probably want a string as an outcome, not the class itself.

This implementation has a glitch: it is hardly reusable, since there is a single shared @number, hence you’d better make all methods as instance methods and do:

class PhoneNumber
  def initialize
    @number = ''
  end
  def prefix
    @number << '+'
    self
  end
  def country
    @number << rand(1..9).to_s
    self
  end
  def code
    @number << rand(100..999).to_s
    self
  end
  def number
    @number << rand(1000000..9999999).to_s
    self
  end
  def to_s
    @number
  end
end

puts PhoneNumber.new.prefix.code.number
#⇒ +6117160297

Now it works for subsequent calls.

like image 34
Aleksei Matiushkin Avatar answered Jun 11 '26 15:06

Aleksei Matiushkin