Working with Ruby
Occasional blog posts about Ruby updates, tools, editor tweaks, and random snippets. You might also be interested in my newer project that docuements lesser-known features in Ruby: Idiosyncratic Ruby.

ActiveSupport 4 by Example: Numeric#to_s

The NumberHelper functionality is now available as Numeric#to_s

To try it out, do

git clone http://github.com/rails/rails
cd rails/activesupport
rvm gemset use rails4 --create
rake gem
gem install pkg/activesupport-4.0.0.beta
irb
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# from https://github.com/rails/rails/blob/master/activesupport/lib \
# /active_support/core_ext/numeric/conversions.rb
>> require 'active_support/core_ext/numeric/conversions' #=> true
>> 42.to_s #=> "42"
>> 13.to_s(:rounded, precision: 5) #=> "13.00000"
>> 12345678.to_s(:delimited) #=> "12,345,678"
>> 98765432.98.to_s(:delimited, delimiter: '.', separator: ',')
=> "98.765.432,98"
>> 55.22.to_s(:percentage, precision: 1) #=> "55.2%"
>> 1234567890123456.to_s(:human) #=> "1.23 Quadrillion"
>> 1234567.to_s(:human_size, precision: 2) #=> "1.2 MB"
>> 1235551234.to_s(:phone) #=> "123-555-1234"
>> 1235551234.to_s(:phone, delimiter: ' ', area_code: true)
=> "(123) 555 1234"
>> 1234567890.50.to_s(:currency) #=> "$1,234,567,890.50"
Creative Commons License