January 2024
Intermediate to advanced
718 pages
20h 15m
English
Symbols don’t have a lot of methods in Ruby. We’ve seen Symbol#to_proc as a shortcut for creating a block. The to_proc method creates a block that is effectively equivalent to { |receiver, ...| receiver.send(symbol, ...)}. You usually see it used implicitly with &, but it can be used explicitly, too:
| | proc = :split.to_proc |
| | proc.call("The pickaxe book") # => ["The", "pickaxe", "book"] |
| | proc.call("The pickaxe book", "e") # => ["Th", " pickax", " book"] |
You can also use Symbol#to_s to convert a symbol to a string; the Symbol#name and Symbol#inspect methods perform basically the same conversion.
Symbols respond to the following string-like methods, which are effectively shortcuts for calling to_s so that you don’t have ...
Read now
Unlock full access