Maurice Kroon
Maurice Kroon

Reputation: 959

ruby string formatting

I'm trying to do some string formatting. I've tried with 'insert' but that is obviously wrong. I only need the formatting.

t = 123456789

I want t to be formatted in: 1234.56.789

So, including the two dots. What is the best way to do this in Ruby?

Upvotes: 0

Views: 199

Answers (1)

forker
forker

Reputation: 2679

extend the class

class String
  def right_format
    self.clone.insert(4,'.').insert(7,'.')
  end
end

i.e. inject a method

Upvotes: 2

Related Questions