Ruby Language Essentials

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

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'

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

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