Ruby Commands
Dynamic language and its reference interpreter.All Ruby pages
- ruby convert string to integer: to_i vs Integer()Convert a string to an integer in Ruby: to_i never fails and returns 0 for bad input, Integer() raises ArgumentError, exception: false returns nil. Bases too.
- ruby parse JSON string to hash (JSON.parse, symbolize_names)Parse a JSON string into a Ruby Hash with JSON.parse, use symbolize_names for Symbol keys, generate and pretty-print JSON, and handle JSON::ParserError.
- ruby round a float to 2 decimal places (round, format, BigDecimal)Round a float to two decimals in Ruby with round(2), show trailing zeros with format("%.2f"), choose half: :even, and use BigDecimal or cents for money.
- ruby sort array of hashes by value (sort_by, min_by, max_by)Sort an array of hashes by a value in Ruby with sort_by, sort descending, sort by several keys, get min_by or max_by, and avoid nil and String-key pitfalls.