Ruby Basics Flashcards
Ruby Basics Flashcards
15 flashcards covering core Ruby concepts. Tap a card to reveal the answer.
Intuition
Ruby is a language designed for developer happiness, everything is an object, and blocks/procs/lambdas make functional patterns feel natural alongside OOP. Metaprogramming is Ruby’s superpower: you can define methods at runtime, open existing classes to add methods, and use method_missing to handle calls to undefined methods. Modules provide mixins for code reuse without multiple inheritance problems.
How to Use These Flashcards
First pass: Go through all 15 cards without rating, just flip and read. This familiarises you with the content and identifies weak areas.
IRB practice: After reviewing a card, open IRB (Interactive Ruby) and experiment with the concept. Ruby’s interactive nature makes it perfect for rapid experimentation.
Rails connection: If you’re learning Ruby for Rails, relate each concept to its Rails use case, blocks for route definitions, mixins for concerns, metaprogramming for ActiveRecord.
Common Pitfalls
- Monkey patching risks: Reopening a class and changing method behaviour affects every instance globally, this can introduce hard-to-debug conflicts between libraries.
- Symbol vs string:
:symboland"string"differ, symbols are immutable and interned (memory efficient), strings are mutable. Use symbols for hash keys and identifiers. - Block vs proc vs lambda: Blocks are not objects, Procs don’t check argument count, and Lambdas do, mixing them up causes subtle argument-passing bugs.
Study Tips
- Use IRB for rapid experimentation: Ruby’s interactive shell is perfect for testing metaprogramming techniques, exploring the object model, and verifying assumptions about method dispatch.
- Read the Ruby style guide: The community style guide (rubystyle.guide) covers conventions that make Ruby code readable and maintainable. Follow it from the beginning.
- Study Rails source code: Rails is a masterclass in Ruby metaprogramming. Reading how ActiveRecord defines dynamic finders or how ActiveRecord::Base uses
method_missingreveals advanced techniques. - Write your own metaprogramming examples: Create a simple class that uses
method_missingto delegate calls, or define methods dynamically withdefine_method. Hands-on experimentation builds understanding. - Practise with blocks and procs: Write functions that accept blocks using
yieldconvert blocks to procs with&blockand use lambdas for strict argument checking. - Explore Ruby 3.x features: Ractors for concurrency, endless methods for brevity, and pattern matching for data destructuring are modern additions worth learning.
- Study the Enumerable module: Ruby’s Enumerable mixin provides powerful iteration methods (map, select, reduce, sort_by). Understanding how to use and extend Enumerable is fundamental to idiomatic Ruby.
- Practise with regular expressions: Ruby’s regex support is excellent. Write patterns for common validation tasks (email, phone, URL) and use
match?, scanandgsubfor string processing.
Cross-References
- Site Home: Main landing page for ruby notes.
- Practice: Practice problems for revision.
Detailed Content
Core Concepts
Worked Examples
Common Mistakes
- Rushing through foundational material
- Not practising problems after reading
- Failing to connect concepts across topics