Skip to content

Instantly share code, notes, and snippets.

@koppen
Last active May 12, 2025 11:20
Show Gist options
  • Save koppen/a5c3464c6b1e2e908006821ad1cfd9b7 to your computer and use it in GitHub Desktop.
Save koppen/a5c3464c6b1e2e908006821ad1cfd9b7 to your computer and use it in GitHub Desktop.
10 Ruby one-liners that will blow Python-developers mind!

10 Ruby one-liners that will blow your mind

Inspired by https://www.facebook.com/techieprogrammer016/videos/1903944506811756

Python Ruby
Swap two variables a, b = b, a a, b = b, a
Reverse a string s[::-1] s.reverse
Check palindrome s == s[::-1] s == s.reverse
Get factorial math.prod(range(1, n+1)) (1..n).inject(:*)
Flatten a list [i for sub in lst for i in sub] lst.flatten
Find even numbers [x for x in range(10) if x % 2 == 0] (0..9).select(&:even?)
Merge two dicts {**d1, **d2} d1.merge(d2)
Count items Counter(lst) lst.count
Get unique elements set(lst) lst.uniq
List to string ''.join(lst) lst.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment