api: YAML JSON TOON Database version: 1.0.0 format: toon dataset: id: 2475 slug: ruby-language-essentials title: Ruby Language Essentials description: "Core Ruby programming concepts: blocks, procs, lambdas, metaprogramming, gems, Rails conventions, and Ruby idioms." summary: "Ruby's essential language concepts: blocks, procs, lambdas, metaprogramming, mixins, Enumerable, singleton methods and method_missing, each with type, syntax, a description and an example." use_when[2]: You are learning Ruby's distinctive features or reading Rails-style code.,"You need the difference between blocks, procs and lambdas." caveats[2]: Metaprogramming is powerful but makes code harder to trace; use it sparingly.,Behavior can differ slightly between Ruby versions; confirm the version in use. related[4]: language-comparison-2026,python-builtins,behavioral-design-patterns,javascript-es6-features entry_count: 8 fields[5]{section,field,type,example}: concepts,name,string,Blocks concepts,type,string,Anonymous code chunks concepts,syntax,string,"{ |args| code } or do...end" concepts,description,string,"Chunks of code passed to methods, fundamental t…" concepts,example,string,"5.times { puts 'Hello' }" category: Programming Languages category_slug: programming-languages tags: "ruby,rails,blocks,procs,lambdas,metaprogramming,gems,idioms" view_count: 42 created_at: 1778695229 updated_at: 1778695229 data: concepts[8]{name,type,syntax,description,example}: Blocks,Anonymous code chunks,"{ |args| code } or do...end","Chunks of code passed to methods, fundamental to Ruby iteration and DSLs","5.times { puts 'Hello' }" Procs,Stored blocks,"Proc.new { |args| code }","Blocks converted to objects, can be stored in variables and passed around","my_proc = Proc.new { |x| x * 2 }; my_proc.call(5)" Lambdas,Strict procs,"->(args) { code } or lambda { |args| code }",Like procs but with strict arity checking and return semantics (like methods),"my_lambda = ->(x) { x + 1 }; my_lambda.call(3)" Metaprogramming,Runtime code generation,"define_method, method_missing, send, class_eval","Writing code that writes code: dynamic method definition, method interception, eval families","define_method(:greet) { |name| puts \"Hello, #{name}\" }" Mixins (Modules),Shared behavior,module MyModule; def method; end; end; class Foo; include MyModule; end,"Share methods across classes without inheritance, avoid multiple inheritance",module Comparable; def <(other); self <=> other == -1; end; end Enumerable,Collection protocol,include Enumerable; def each; end,"Module providing iteration methods (map, select, reduce) to any class implementing #each",class MyCollection; include Enumerable; def each(&block); items.each(&block); end; end Singleton Methods,Per-object methods,"def obj.method; end or obj.define_singleton_method(:m) { }","Methods defined on a single object instance, not its class",str = 'hello'; def str.shout; self.upcase + '!'; end; str.shout # => 'HELLO!' Method Missing,Dynamic dispatch,"def method_missing(name, *args, &block); end","Intercept calls to undefined methods for DSLs, proxies, and dynamic behavior",def method_missing(name); name.to_s.reverse; end; obj.hello #=> 'olleh'