Feature #8573

Add String#format method(not an alias of String#%)

Added by Bozhidar Batsov 10 months ago. Updated 7 months ago.

[ruby-core:55682]
Status:Feedback
Priority:Normal
Assignee:-
Category:lib
Target version:next minor

Description

I think it'd be great if String included a method similar to % that accepts variable number of arguments (like sprintf). This is trivial to implement as you're certainly aware.

Here's one implementation suggestion:

class String
def format(*args)
super(self, *(args.flatten))
end
end

This would make the new method behave both like sprintf/format and String#%. I think this minor improvement would definitely be in the spirit of making more programmers happy :-)

Here a bit more on the topic - http://batsov.com/articles/2013/06/27/the-elements-of-style-in-ruby-number-2-favor-sprintf-format-over-string-number-percent/

History

#1 Updated by Nobuyoshi Nakada 7 months ago

=begin
Do you mean this?

def format(args)
sprintf(self, *args)
end
or
 def format(
args)
   self % args
 end
=end

#2 Updated by Yui NARUSE 7 months ago

  • Status changed from Open to Feedback
  • Priority changed from Low to Normal
  • Target version changed from 2.1.0 to next minor

Show concrete use case.

Also available in: Atom PDF