Duck typing and core idioms

Contents
If it quacks
To_s and inspect
Comparable and sorting
Freeze constants

If it quacks

Duck typing means Ruby cares about methods, not class names. If an object responds to quack, the caller treats it as a duck.

This keeps code flexible: new classes work with old methods without any shared parent. Collections of such objects are easy to process with Enumerables.

def greet(thing) puts "Hello, " + thing.to_s end greet("Ada") greet(42)

To_s and inspect

to_s gives the friendly reading form: the text you show to users. inspect gives the programmer form with extra marks like quotes.

puts calls to_s, while p calls inspect. During debugging, p shows you the real structure.

n = 42 puts n.to_s p n

Comparable and sorting

Include Comparable and define <=> to gain sorting and all six comparison checks from one definition.

The method returns minus one, zero, or plus one depending on order. sort uses it automatically.

class Size include Comparable attr_reader :cm def initialize(cm) @cm = cm end def <=>(other) @cm <=> other.cm end end puts Size.new(3) < Size.new(9)

Freeze constants

Constants can still change inside when they point to arrays or strings. Freeze them so accidental edits raise an error instead of corrupting state.

Capitalized names warn on reassignment but do not lock the object. Only freeze locks the content.

COLORS = ["red", "blue"].freeze APP = "aredel".freeze

Next: Gems and Bundler

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: