Ruby Language Essentials

Core Ruby programming concepts: blocks, procs, lambdas, metaprogramming, gems, Rails conventions, and Ruby idioms.

  • JSON5,604 B
  • YAML4,010 B · −28%
  • TOON3,269 B · −42%

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 it when

  • You are learning Ruby's distinctive features or reading Rails-style code.
  • You need the difference between blocks, procs and lambdas.

Watch out for

  • Metaprogramming is powerful but makes code harder to trace; use it sparingly.
  • Behavior can differ slightly between Ruby versions; confirm the version in use.

The data

Concepts

NameTypeSyntaxDescriptionExample
BlocksAnonymous code chunks{ |args| code } or do...endChunks of code passed to methods, fundamental to Ruby iteration and DSLs5.times { puts 'Hello' }
ProcsStored blocksProc.new { |args| code }Blocks converted to objects, can be stored in variables and passed aroundmy_proc = Proc.new { |x| x * 2 }; my_proc.call(5)
LambdasStrict 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)
MetaprogrammingRuntime code generationdefine_method, method_missing, send, class_evalWriting code that writes code: dynamic method definition, method interception, eval familiesdefine_method(:greet) { |name| puts "Hello, #{name}" }
Mixins (Modules)Shared behaviormodule MyModule; def method; end; end; class Foo; include MyModule; endShare methods across classes without inheritance, avoid multiple inheritancemodule Comparable; def <(other); self <=> other == -1; end; end
EnumerableCollection protocolinclude Enumerable; def each; endModule providing iteration methods (map, select, reduce) to any class implementing #eachclass MyCollection; include Enumerable; def each(&block); items.each(&block); end; end
Singleton MethodsPer-object methodsdef obj.method; end or obj.define_singleton_method(:m) { }Methods defined on a single object instance, not its classstr = 'hello'; def str.shout; self.upcase + '!'; end; str.shout # => 'HELLO!'
Method MissingDynamic dispatchdef method_missing(name, *args, &block); endIntercept calls to undefined methods for DSLs, proxies, and dynamic behaviordef method_missing(name); name.to_s.reverse; end; obj.hello #=> 'olleh'

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
conceptsnamestringBlocks
conceptstypestringAnonymous code chunks
conceptssyntaxstring{ |args| code } or do...end
conceptsdescriptionstringChunks of code passed to methods, fundamental t…
conceptsexamplestring5.times { puts 'Hello' }

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.com/api/dataset/ruby-language-essentials?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/ruby-language-essentials.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • ruby
  • rails
  • blocks
  • procs
  • lambdas
  • metaprogramming
  • gems
  • idioms

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets