A complete demonstration of Ruby language features, idioms, and best practices in a single file.
KitchenSink.rb
is an educational resource that showcases virtually every Ruby language feature with idiomatic examples. This file serves as both a reference guide and a learning tool for Ruby developers of all levels.
- Data Types: Integers, Floats, Strings, Symbols, Booleans, nil, Ranges, Regular Expressions
- Collections: Arrays, Hashes, Sets, and their various initialization methods
- Variables: Local, instance, class, global variables, and constants
- Operators: Arithmetic, comparison, logical, bitwise, assignment, splat, and safe navigation
- Conditionals: if/elsif/else, unless, ternary operator, case statements
- Pattern Matching: Ruby 2.7+ pattern matching syntax
- Loops: while, until, for, loop with break/next, times, each
- Classes: Constructors, instance/class methods, attributes, visibility modifiers
- Inheritance: Single inheritance, method overriding, super
- Modules: Mixins (include), class methods (extend), prepend, module functions
- Structs: Basic and keyword-initialized structs with custom methods
- Blocks: Single-line and multi-line blocks
- Procs and Lambdas: Creation, calling conventions, currying
- Higher-Order Functions: Function composition, partial application
- Lazy Evaluation: Infinite sequences and lazy enumerators
- Metaprogramming: define_method, method_missing, class_eval, instance_eval
- Reflection: Object introspection, method objects, constant manipulation
- Refinements: Scoped monkey patching for safer extensions
- Operator Overloading: Custom operators for user-defined classes
Comprehensive examples of:
- map, select, reject, reduce, find
- any?, all?, none?, one?
- partition, group_by, sort_by
- zip, cycle, take, drop
- Lazy evaluation chains
- File I/O: Reading, writing, file operations with blocks
- Directory Operations: Navigation, listing, globbing
- JSON/YAML: Parsing and generation
- Command Line: ARGV processing, OptionParser
- Basic Handling: begin/rescue/else/ensure blocks
- Custom Exceptions: Creating custom error classes
- Control Flow: throw/catch, retry mechanisms
- Threads: Basic threads, thread synchronization with Mutex
- Fibers: Lightweight concurrency, generators
- Thread-Local Storage: Thread-specific variables
- Environment Variables: Reading and setting ENV
- Process Management: Command line arguments, system interaction
- Benchmarking: Performance measurement and comparison
- Garbage Collection: GC control and statistics
ruby KitchenSink.rb
Browse through the source code to find examples of specific Ruby features. Each section is clearly commented and organized by topic.
- Read through sections sequentially to understand Ruby's features
- Copy and modify examples for your own experimentation
- Use as a quick reference when you need syntax reminders
The file is organized into clearly marked sections:
- Basic Data Types and Literals
- Collections
- Variables and Constants
- Operators
- Control Flow
- Loops
- Methods
- Blocks, Procs, and Lambdas
- Classes
- Modules
- Structs
- Enumerables and Iterators
- File I/O
- Exceptions
- Metaprogramming
- Reflection
- Threads and Concurrency
- Regular Expressions
- Date and Time
- And many more...
- Ruby 2.7 or higher (for pattern matching support)
- Some examples use standard library gems (json, yaml, set, etc.)
This file is perfect for:
- Beginners: Learning Ruby syntax and idioms
- Intermediate Developers: Discovering lesser-known features
- Advanced Developers: Quick reference for specific syntax
- Teachers: Comprehensive examples for instruction
- Code Reviews: Understanding idiomatic Ruby patterns
While this is a comprehensive collection, Ruby is always evolving. If you notice missing features or have suggestions for better examples, please feel free to contribute!
This educational resource is provided as-is for learning purposes. Feel free to use, modify, and share these examples in your own projects and educational materials.
- All examples are self-contained and runnable
- Error handling is included where appropriate to prevent crashes
- Examples favor clarity and education over performance
- Some features may require specific Ruby versions or gems
Happy Ruby learning! 💎