
Senior Software Engineer @moneyforward.
- Tweets243
- Following236
- Followers3.5K
- Likes1.2K

ALT # Make a number always show 2 decimals (useful for money) "%.2f" % 10 # => "10.00" "%.2f" % 10.50 # => "10.50" # You can even add the currency sign "$%.2f" % 10.50 # => "$10.50" # Convert to hexadecimal "%#x" % 1234 # => "0x4d2" # And it can do a lot more! # % is a shortcut that uses Kernel#sprintf behind the scenes # so if you're looking for the format specification, check # out the official ruby doc for Kern...
![array = [nil, "", 2]
# To convert all values to strings
# instead of
array.map { |value| value.to_s }
# you can do
array.map(&:to_s)
# => ["", "", 2]
# Or to remove any nil or empty values
array.find_all(&:present?)
# => [2]
# The trick is that the ampersand(&)
# operator will convert to a proc
# and give it as a block. Converting
# a symbol to a proc will call the
# method on the param.
:to_s.to_proc.call(1)
# ...](https://pbs.twimg.com/media/FPWMOO7VUAISNyC.jpg?name=small&format=webp)
ALT array = [nil, "", 2] # To convert all values to strings # instead of array.map { |value| value.to_s } # you can do array.map(&:to_s) # => ["", "", 2] # Or to remove any nil or empty values array.find_all(&:present?) # => [2] # The trick is that the ampersand(&) # operator will convert to a proc # and give it as a block. Converting # a symbol to a proc will call the # method on the param. :to_s.to_proc.call(1) # ...
![# db/migrate/20220226153929_create_orders.rb
class CreateOrders < ActiveRecord::Migration[7.0]
def change
create_table :orders do |t|
t.integer :subtotal_cents
t.integer :tax_cents
t.virtual :total_cents, type: :integer, as: "subtotal_cents + tax_cents", stored: true
t.timestamps
end
end
end
# console
Order.create(subtotal_cents: 1000, tax_cents: 200)
Order.last.total_ce...](https://pbs.twimg.com/media/FNGAInSVgAMi-za.jpg?name=small&format=webp)
ALT # db/migrate/20220226153929_create_orders.rb class CreateOrders < ActiveRecord::Migration[7.0] def change create_table :orders do |t| t.integer :subtotal_cents t.integer :tax_cents t.virtual :total_cents, type: :integer, as: "subtotal_cents + tax_cents", stored: true t.timestamps end end end # console Order.create(subtotal_cents: 1000, tax_cents: 200) Order.last.total_ce...

ALT # config/application.rb config.active_record.async_query_executor = :global_thread_pool # :multi_thread_pool executor can be used if you have multiple databases # app/controllers/dashboards_controller.rb class DashboardsController < ApplicationController def show # These queries will all run asynchronously # instead of running one by one @posts = Post.limit(500).load_async @users = User.limit(50...










![class CreateEvents < ActiveRecord::Migration[6.1]
def change
create_table :events do |t|
t.string :name
t.interval :duration
t.timestamps
end
end
end](https://pbs.twimg.com/media/FK16ZFRVQAIQhKE.jpg?name=small&format=webp)
