Powerful switch statement with pattern matching in Ruby ruby

Powerful switch statement with pattern matching in Ruby

Pattern matching in Ruby turns case statements into a precise switch over data shapes: match and destructure hashes from an API response, combine several patterns in one branch, capture values across patterns, and validate configuration. Ruby 2.7+ syntax examples.

Jul 17, 2023

How to set up XML in Grape API

How to set up XML in Grape API

XML support in Grape API takes two steps: register the additional content_type so endpoints accept and return application/xml, then change the root element name of the XML output. Complete Ruby examples built around a products endpoint you can adapt to your API.

Multiple ways to execute a shell command via Ruby

Multiple ways to execute a shell command via Ruby

Running shell commands from Ruby: system, backticks, exec, the IO class, spawn, and Open3.popen3 compared with pros and cons, from quick one-liners to capturing stdout, stderr, and exit status, so you can pick the right method for scripts and production code.

Understanding generic types in Typescript

Understanding generic types in Typescript

Generic types in TypeScript let one function, class, or interface work safely with many data types. What generics are, how a type parameter beats hardcoding a concrete entity type in a validation helper, and honest pros and cons before you use them everywhere.

Ruby in browser via WebAssembly

Ruby in browser via WebAssembly

Ruby 3.2 runs in the browser through WebAssembly: embed Ruby in a script tag with ruby-wasm-wasi or drive the Ruby VM from JavaScript and mix it with HTML. Working examples plus honest limits: no threading or networking, and no extra gems without a custom wasm image.

Solutions for const and enums per class in Ruby

Solutions for const and enums per class in Ruby

Three ways to define constants and enums per class in Ruby: the ActiveRecord enum macro, a mixed style combining frozen constants with enum, and a dependency-free PORO version. Code for each solution and a comparison to help you pick the right one.

Factory Pattern with auto-detecting classes in Ruby

Factory Pattern with auto-detecting classes in Ruby

The Factory Pattern in Ruby, then a step further: using naming conventions and regular expressions so the factory auto-detects which notification class to instantiate. Complete code for both versions, plus when strict class listing is the safer choice.

Map vs WeakMap in practice

Map vs WeakMap in practice

Map vs WeakMap in JavaScript comes down to keys and memory: WeakMap only accepts object keys and lets garbage collection reclaim unused entries, while Map holds onto everything. Differences, similarities, and practical guidance on when each one is the right choice.

Anonymous Arguments Passing Improvements in Ruby 3.2

Anonymous Arguments Passing Improvements in Ruby 3.2

Ruby 3.2 adds new syntax for anonymous arguments: forward positional and keyword arguments with bare * and ** and unpack them inside the method, no more digging through kwargs hashes. A before-and-after example shows how much cleaner argument passing gets.