ruby
✓Verified·Scanned 2/18/2026
Write expressive Ruby with blocks, metaprogramming, and idiomatic patterns.
from clawhub.ai·v40f0089·3.8 KB·0 installs
Scanned from 1.0.0 at 40f0089 · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/ruby
Ruby Development Rules
Blocks and Procs
do...endfor multi-line,{}for single line — convention, both workyieldcalls the block —block_given?checks if block passed&blockcaptures block as Proc — can store, pass around, call later- Lambda checks argument count, Proc doesn't — lambda returns from lambda, Proc returns from enclosing method
->is stabby lambda syntax —add = ->(a, b) { a + b }
Methods
- Last expression is implicit return — no
returnneeded unless early exit ?suffix for boolean methods —empty?,valid?, convention not enforcement!suffix for dangerous methods — usually mutates in place or raises instead of returning nil- Parentheses optional — omit for DSLs and zero-arg, use for clarity with arguments
*argssplat for variable arguments —**kwargsfor keyword arguments
Truthiness
- Only
nilandfalseare falsy —0,"",[]are truthy ||=for memoization —@user ||= fetch_usercaches on first call&&and||return actual values — not booleans, useful for defaultsnil?checks nil specifically —blank?is Rails (nil, empty, whitespace)
Symbols vs Strings
- Symbols are immutable, interned — same
:nameis same object everywhere - Symbols for keys, identifiers — strings for text content
- Symbol keys in hashes:
{ name: "Ruby" }— shorthand for{ :name => "Ruby" } to_symandto_sconvert — but don't convert user input to symbols (memory leak)
Collections
eachfor iteration —mapfor transformationselect/rejectfor filtering — return new arrayfindreturns first match —nilif none, not exceptionreduce/injectfor accumulation —[1,2,3].reduce(0) { |sum, n| sum + n }compactremoves nils —flattencollapses nested arrays
Classes
attr_accessorfor getter and setter —attr_readerread-only,attr_writerwrite-onlyinitializeis constructor — instance variables start with@self.methodfor class methods — orclass << selfblock for multiple- Inheritance with
<—class Dog < Animal - Modules for mixins —
includeadds instance methods,extendadds class methods
Metaprogramming
method_missingcatches undefined calls — always definerespond_to_missing?toodefine_methodcreates methods dynamically — takes name and blocksendcalls method by name —obj.send(:method_name, args)- Open classes: can add methods to any class — use responsibly, prefer refinements
class_evalandinstance_eval— change self for block evaluation
Scope
def,class,modulecreate scope gates — block variables leak in older Ruby, not anymore- Constants are UPPERCASE — looked up lexically then by inheritance
::for namespacing —Module::Class::CONSTANT- Global variables
$are code smell — avoid except built-ins like$stdout
Common Mistakes
==for equality,equal?for identity — opposite-ish of Java- String interpolation only in double quotes —
"Hello #{name}"not'Hello #{name}' - Modifying while iterating is allowed — but confusing, prefer
map/selectto build new - Ranges:
1..5includes 5,1...5excludes — three dots exclude end requirevsrequire_relative— relative for same project, require for gems
Gems and Bundler
Gemfilelists dependencies —bundle installinstalls themGemfile.lockpins versions — commit it for reproducible buildsbundle execruns with bundled gems — avoids version conflicts- Semantic versioning:
~> 2.1allows 2.x —>= 2.1, < 3.0equivalent