api: "YAML JSON TOON Database" version: 1.0.0 format: json dataset: id: 50 slug: ruby-language-essentials title: "Ruby Language Essentials" description: "Core Ruby programming concepts: blocks, procs, lambdas, metaprogramming, gems, Rails conventions, and Ruby idioms." category: "Programming Languages" category_slug: programming-languages tags: "ruby,rails,blocks,procs,lambdas,metaprogramming,gems,idioms" view_count: 0 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'"