[Ruby] Realize 3-digit separation display in Ruby

less than 1 minute read

When you want to display #3 digits

  1. Use to_s(:delimited) method
  2. Add require at the beginning of the file to use the above method active_support is a component of Ruby on Rails that allows you to use various methods

``'’ruby:num.rb require “active_support/core_ext/numeric/conversions”

10000.to_s(:delimited)

=> “10,000”



See the Rails Guide Active Support page for more details (6.3 formatting)
https://railsguides.jp/active_support_core_extensions.html#%E6%9B%B8%E5%BC%8F%E8%A8%AD%E5%AE%9A


# Postscript
When I was looking for a 3 digit display method
I used it happily as "It seems that the number can be separated by 3 digits with the .to_s(:delimited) method!"
I didn't know that require was needed and encountered the following error.

in to_s': no implicit conversion of Symbol into Integer (TypeError) ``` Although to_s can be used normally, adding (:delimited) caused an error and caused a problem. Therefore, require to activate active_support is required when using to_s(:delimited) method in ruby.

I would like to take this opportunity to learn and utilize other useful methods of active_support.