ruby.onl / snippets

The Ruby vs Perl Quick Reference Card

2026-03-08

Everything in one place. Variables, switches, quoting, output, strings, arrays, hashes, syntactic sugar, input sources, file operations. Print this, laminate it, keep it next to your keyboard. This is the cheat sheet to end all cheat sheets.

Part 1: Variables

Purpose Perl Ruby
Current line $_ $_
Split fields @F $F
Line number $. $.
Input separator $/ $/
Output separator $\ $\
Match result $& $&
Capture groups $1, $2... $1, $2...
Process ID $$ $$
Program name $0 $0
Loaded features %INC $" or $LOADED_FEATURES
Child error $? $?
Regex prematch $` $`
Regex postmatch $' $'

Part 2: Command-Line Switches

Switch Perl Ruby Notes
Execute code -e -e Same
Loop lines -n -n Same (wraps in while gets)
Loop + print -p -p Same
Auto-split -a -a Perl: @F, Ruby: $F
Field separator -F -F Same
In-place edit -i -i Same
Auto-chomp -l -l Same
Record separator -0 -0 Same

Part 3: String Quoting

Type Perl Ruby Interpolation?
Single-quoted q~~ %q~~ No
Double-quoted qq~~ %Q~~ Yes
Word array qw() %w~~ No
Word array (interp) N/A %W~~ Yes
Regex m~~ or qr~~ %r~~ Yes
Shell command qx() %x~~ Yes
Symbol array N/A %i~~ No

Part 4: Output

Purpose Perl Ruby
Print with newline say puts
Print without newline print print
Formatted print printf printf
Sprint format sprintf sprintf or "fmt" % args
Stderr warn STDERR.puts or warn

Part 5: String Operations

Operation Perl Ruby
Substitution s/old/new/g str.gsub(%r~old~, "new")
First substitution s/old/new/ str.sub(%r~old~, "new")
Match test $str =~ m/pat/ str =~ %r~pat~
All matches @matches = $str =~ m/pat/g str.scan(%r~pat~)
First match extract ($match) = $str =~ m/(pat)/ str[%r~(pat)~, 1]
Chomp chomp $str str.chomp!
Strip whitespace $str =~ s/^\s+|\s+$//g str.strip!
Length length($str) str.length or str.size
Substring substr($str, 2, 5) str[2, 5] or str[2..6]
Split split /,/, $str str.split(",")
Join join(",", @arr) arr.join(",")
Upper/Lower uc($str) / lc($str) str.upcase / str.downcase

Part 6: Array Operations

Operation Perl Ruby
Push push @arr, $val arr.push(val) or arr << val
Pop pop @arr arr.pop
Shift shift @arr arr.shift
Unshift unshift @arr, $val arr.unshift(val)
Sort sort @arr arr.sort
Reverse reverse @arr arr.reverse
Unique Custom / List::Util arr.uniq
Grep/Select grep { /pat/ } @arr arr.select { |x| x =~ %r~pat~ }
Map map { uc } @arr arr.map(&:upcase)
Size scalar @arr arr.size or arr.length
Slice @arr[2..5] arr[2..5]

Part 7: Hash Operations

Operation Perl Ruby
Create my %h = (k => 'v') h = { k: 'v' }
Access $h{key} h[:key] or h["key"]
Exists exists $h{key} h.key?(:key)
Delete delete $h{key} h.delete(:key)
Keys keys %h h.keys
Values values %h h.values
Each while (($k,$v) = each %h) h.each { |k,v| ... }
Default value N/A (autovivify) Hash.new(0) or block form
Counting $h{$key}++ h[key] += 1 (with default 0)
Merge %h = (%h1, %h2) h1.merge(h2)

Part 8: Syntactic Sugar

Feature Ruby Syntax Perl Equivalent
Symbol-to-Proc arr.map(&:upcase) map { uc } @arr
Safe navigation obj&.method $obj && $obj->method
Conditional assign x ||= 0 $x //= 0
Flip-flop if a..b if /a/../b/
Numbered params _1, _2 N/A
Splat destructure first, *rest = arr ($first, @rest) = @arr
Ternary a ? b : c $a ? $b : $c
Trailing if puts x if cond print $x if $cond
Trailing unless next unless cond next unless $cond
Here-doc (strip) <<~END <<END (no auto-strip)
Autovivify hash Hash.new { block } Native
ARGF / diamond ARGF or $< <>
Tally/count arr.tally Manual hash counting
Pattern matching case/in (Ruby 3+) N/A

Part 9: Input Sources

Source Perl Ruby
Stdin <STDIN> STDIN.gets or $stdin
Files from args <> ARGF
Stdin or files <> ARGF (same behavior)
Current filename $ARGV ARGF.filename
Read all at once do { local $/; <> } ARGF.read
Read all lines @lines = <> ARGF.readlines

Part 10: File Operations

Operation Perl Ruby
Open read open(FH, '<', $f) File.open(f, 'r')
Open write open(FH, '>', $f) File.open(f, 'w')
Read all do { local $/; <FH> } File.read(f)
Read lines @lines = <FH> File.readlines(f)
Write print FH $data File.write(f, data)
File exists -e $file File.exist?(f)
File size -s $file File.size(f)
Directory check -d $path File.directory?(p)
Glob glob("*.txt") Dir.glob("*.txt")
Keep this card handy. The first week you'll reference it constantly. By the second week, you'll only need it for the edge cases. By the third week, you'll be writing Ruby from memory and wondering why you didn't switch sooner.

Created By: Wildcard Wizard. Copyright 2026