-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKitchenSink.rb
More file actions
1283 lines (1031 loc) · 27.8 KB
/
KitchenSink.rb
File metadata and controls
1283 lines (1031 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# frozen_string_literal: true
# KitchenSink.rb - Comprehensive Ruby Examples
# This file demonstrates idiomatic Ruby code for virtually every feature
# ============================================================================
# BASIC DATA TYPES AND LITERALS
# ============================================================================
# Numbers
integer = 42
big_integer = 1_000_000_000
binary = 0b1010
octal = 0o755
hex = 0xFF
float = 3.14159
scientific = 1.23e-4
rational = Rational(3, 4)
complex = Complex(2, 3)
# Strings
single_quote = 'Hello, World!'
double_quote = "Hello, #{integer}"
multiline = <<~HEREDOC
This is a multiline string
with proper indentation
HEREDOC
percent_string = %{This is another way to write strings}
escaped = "Line 1\nLine 2\tTabbed"
# Symbols
symbol = :ruby
dynamic_symbol = :"dynamic_#{integer}"
# Booleans and nil
truthy = true
falsy = false
nothing = nil
# Ranges
inclusive = 1..10
exclusive = 1...10
letter_range = 'a'..'z'
# Regular Expressions
simple_regex = /pattern/
multiline_regex = /^start.*end$/m
case_insensitive = /ruby/i
interpolated_regex = /#{integer}/
# ============================================================================
# COLLECTIONS
# ============================================================================
# Arrays
empty_array = []
literal_array = [1, 2, 3, 4, 5]
word_array = %w[apple banana cherry]
symbol_array = %i[one two three]
nested_array = [[1, 2], [3, 4], [5, 6]]
range_to_array = (1..5).to_a
# Hashes
empty_hash = {}
symbol_hash = { name: 'Ruby', version: 3.0 }
arrow_hash = { 'key' => 'value', 'another' => 'pair' }
nested_hash = { user: { name: 'Alice', age: 30 } }
# Sets
require 'set'
unique_set = Set[1, 2, 3, 3, 4]
set_from_array = [1, 2, 2, 3].to_set
# ============================================================================
# VARIABLES AND CONSTANTS
# ============================================================================
LOCAL_CONSTANT = 'immutable'
$global_variable = 'accessible everywhere'
@@class_variable = 'shared across class instances'
@instance_variable = 'specific to instance'
local_variable = 'method or block scope'
# ============================================================================
# OPERATORS
# ============================================================================
# Arithmetic
sum = 10 + 5
difference = 10 - 5
product = 10 * 5
quotient = 10 / 5
modulo = 10 % 3
power = 2**8
# Comparison
equal = 5 == 5
not_equal = 5 != 4
less_than = 3 < 5
greater_than = 5 > 3
less_or_equal = 3 <= 3
greater_or_equal = 5 >= 5
spaceship = 5 <=> 3 # Returns -1, 0, or 1
# Logical
and_result = true && false
or_result = true || false
not_result = !true
# Bitwise
bitwise_and = 5 & 3
bitwise_or = 5 | 3
bitwise_xor = 5 ^ 3
bitwise_not = ~5
left_shift = 5 << 1
right_shift = 5 >> 1
# Assignment operators
x = 10
x += 5 # x = x + 5
x -= 3 # x = x - 3
x *= 2 # x = x * 2
x /= 4 # x = x / 4
x ||= 20 # x = x || 20 (assign if nil or false)
x &&= 30 # x = x && 30 (assign if truthy)
# Splat operators
*first_items, last = [1, 2, 3, 4]
first, *middle, last = [1, 2, 3, 4, 5]
double_splat = { **symbol_hash, extra: 'value' }
# Safe navigation
safe_nav = nil&.upcase # Won't raise error
# ============================================================================
# CONTROL FLOW
# ============================================================================
# If statements
if true
puts 'True branch'
elsif false
puts 'Elsif branch'
else
puts 'Else branch'
end
# Unless
unless false
puts 'Executes when condition is false'
end
# Ternary operator
result = true ? 'yes' : 'no'
# Case statements
grade = 85
letter_grade = case grade
when 90..100 then 'A'
when 80..89 then 'B'
when 70..79 then 'C'
when 60..69 then 'D'
else 'F'
end
# Case with multiple conditions
day = 'Monday'
type = case day
when 'Saturday', 'Sunday'
'Weekend'
when 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
'Weekday'
end
# Pattern matching (Ruby 2.7+)
case [1, 2, 3]
in [1, 2, 3]
'Exact match'
in [1, *rest]
"Starts with 1, rest: #{rest}"
in [*elements]
"Any array: #{elements}"
end
# ============================================================================
# LOOPS
# ============================================================================
# While loop
counter = 0
while counter < 5
counter += 1
end
# Until loop
until counter == 0
counter -= 1
end
# For loop
for i in 1..5
# Do something
end
# Loop with break and next
loop do
break if counter > 10
next if counter.even?
counter += 1
end
# Times
5.times { |i| puts i }
# Each
[1, 2, 3].each { |n| puts n }
# Each with index
%w[a b c].each_with_index { |item, index| puts "#{index}: #{item}" }
# ============================================================================
# METHODS
# ============================================================================
# Basic method
def simple_method
'Hello'
end
# Method with parameters
def greet(name, greeting = 'Hello')
"#{greeting}, #{name}!"
end
# Method with keyword arguments
def create_user(name:, age:, email: nil)
{ name: name, age: age, email: email }
end
# Method with splat parameters
def sum(*numbers)
numbers.reduce(0, :+)
end
# Method with double splat
def build_options(**opts)
opts
end
# Method with block
def with_timing
start = Time.now
yield if block_given?
Time.now - start
end
# Method with explicit block parameter
def explicit_block(&block)
block.call if block
end
# Method with multiple return values
def multiple_returns
[1, 2, 3]
end
# Recursive method
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
# ============================================================================
# BLOCKS, PROCS, AND LAMBDAS
# ============================================================================
# Blocks
[1, 2, 3].map { |n| n * 2 }
[1, 2, 3].map do |n|
n * 2
end
# Proc
my_proc = Proc.new { |x| x * 2 }
my_proc.call(5)
my_proc[5]
my_proc.(5)
# Lambda
my_lambda = ->(x) { x * 2 }
my_lambda.call(5)
# Lambda with multiple arguments
add = ->(a, b) { a + b }
add.call(2, 3)
# Currying
multiply = ->(x, y) { x * y }
double = multiply.curry[2]
double[5] # Returns 10
# ============================================================================
# CLASSES
# ============================================================================
class Person
# Class variable
@@count = 0
# Class constant
SPECIES = 'Homo sapiens'
# attr_accessor creates getter and setter
attr_accessor :name
# attr_reader creates only getter
attr_reader :age
# attr_writer creates only setter
attr_writer :email
# Constructor
def initialize(name, age)
@name = name
@age = age
@@count += 1
end
# Instance method
def introduce
"Hi, I'm #{@name} and I'm #{@age} years old."
end
# Class method
def self.count
@@count
end
# Method with visibility
private
def secret_method
'This is private'
end
protected
def protected_method
'This is protected'
end
public
def public_method
'This is public'
end
end
# Inheritance
class Employee < Person
attr_accessor :employee_id
def initialize(name, age, employee_id)
super(name, age)
@employee_id = employee_id
end
# Method overriding
def introduce
"#{super} I work here with ID: #{@employee_id}"
end
end
# Class with class methods using self
class Calculator
class << self
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
end
end
# Singleton class
class Singleton
private_class_method :new
def self.instance
@instance ||= new
end
end
# ============================================================================
# MODULES
# ============================================================================
module Greetable
# Module constant
DEFAULT_GREETING = 'Hello'
# Module method
def say_hello
"#{DEFAULT_GREETING} from module"
end
# Module function (can be called on module itself)
module_function
def module_method
'This can be called on the module'
end
end
# Module as namespace
module Utils
module StringUtils
def self.reverse(str)
str.reverse
end
end
module MathUtils
def self.square(n)
n**2
end
end
end
# Mixin
class Greeter
include Greetable
def greet
say_hello
end
end
# Extend (adds as class methods)
class ClassGreeter
extend Greetable
end
# Prepend (inserts before class in method lookup)
module Countable
def increment
@count = (@count || 0) + 1
super if defined?(super)
end
end
class Counter
prepend Countable
def increment
puts 'Original increment'
end
end
# ============================================================================
# STRUCTS
# ============================================================================
# Basic Struct
Person = Struct.new(:name, :age)
person = Person.new('Alice', 30)
# Struct with keyword arguments
Dog = Struct.new(:name, :breed, keyword_init: true)
dog = Dog.new(name: 'Buddy', breed: 'Golden Retriever')
# Struct with custom methods
Cat = Struct.new(:name, :age) do
def meow
"#{name} says meow!"
end
end
# ============================================================================
# ENUMERABLES AND ITERATORS
# ============================================================================
numbers = [1, 2, 3, 4, 5]
# Map/Collect
doubled = numbers.map { |n| n * 2 }
collected = numbers.collect { |n| n * 2 }
# Select/Filter
evens = numbers.select { |n| n.even? }
filtered = numbers.filter { |n| n > 2 }
# Reject
odds = numbers.reject { |n| n.even? }
# Reduce/Inject
sum = numbers.reduce(:+)
product = numbers.inject(1) { |acc, n| acc * n }
# Find/Detect
first_even = numbers.find { |n| n.even? }
detected = numbers.detect { |n| n > 3 }
# Any?, All?, None?, One?
has_even = numbers.any?(&:even?)
all_positive = numbers.all?(&:positive?)
no_negatives = numbers.none?(&:negative?)
one_five = numbers.one? { |n| n == 5 }
# Partition
evens, odds = numbers.partition(&:even?)
# Group_by
grouped = %w[apple apricot banana blueberry cherry].group_by { |word| word[0] }
# Sort and Sort_by
sorted = [3, 1, 4, 1, 5, 9].sort
sorted_by_length = %w[apple pie cherry].sort_by(&:length)
# Zip
letters = %w[a b c]
zipped = numbers.zip(letters)
# Cycle
cycled = [1, 2, 3].cycle(2).to_a
# Take and Drop
first_three = numbers.take(3)
last_two = numbers.drop(3)
# Lazy evaluation
lazy_result = (1..Float::INFINITY).lazy
.map { |n| n * 2 }
.select { |n| n % 3 == 0 }
.take(5)
.to_a
# ============================================================================
# FILE I/O
# ============================================================================
# Reading files
content = File.read('filename.txt') rescue 'File not found'
lines = File.readlines('filename.txt') rescue []
# Writing files
File.write('output.txt', 'Hello, World!') rescue nil
# File operations with blocks
File.open('file.txt', 'w') do |file|
file.puts 'Line 1'
file.puts 'Line 2'
end rescue nil
# File existence and info
exists = File.exist?('file.txt')
size = File.size('file.txt') rescue 0
directory = File.directory?('.')
basename = File.basename('/path/to/file.txt')
dirname = File.dirname('/path/to/file.txt')
extname = File.extname('file.txt')
# Directory operations
Dir.pwd
Dir.entries('.') rescue []
Dir.glob('*.rb')
Dir.mkdir('new_directory') rescue nil
# ============================================================================
# EXCEPTIONS
# ============================================================================
# Basic exception handling
begin
# Code that might raise an exception
risky_operation = 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
rescue StandardError => e
puts "General error: #{e.message}"
else
puts 'No exception raised'
ensure
puts 'This always executes'
end
# Raising exceptions
def validate_age(age)
raise ArgumentError, 'Age must be positive' if age < 0
age
end
# Custom exception classes
class CustomError < StandardError
attr_reader :code
def initialize(message, code)
super(message)
@code = code
end
end
# Retry
retries = 0
begin
# Code that might fail
rescue StandardError
retries += 1
retry if retries < 3
end
# Throw and catch
result = catch(:done) do
[1, 2, 3].each do |i|
[4, 5, 6].each do |j|
throw :done, [i, j] if i * j == 12
end
end
end
# ============================================================================
# METAPROGRAMMING
# ============================================================================
# define_method
class DynamicClass
define_method :dynamic_method do |arg|
"Dynamic with #{arg}"
end
# Creating methods dynamically
%w[create read update delete].each do |action|
define_method "#{action}_record" do |id|
"#{action.capitalize} record #{id}"
end
end
end
# method_missing
class GhostClass
def method_missing(method_name, *args)
if method_name.to_s.start_with?('find_by_')
attribute = method_name.to_s.sub('find_by_', '')
"Finding by #{attribute}: #{args.first}"
else
super
end
end
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('find_by_') || super
end
end
# Class eval and instance eval
String.class_eval do
def shout
upcase + '!'
end
end
object = Object.new
object.instance_eval do
def singleton_method
'I am unique to this instance'
end
end
# Send
'hello'.send(:upcase)
'hello'.public_send(:upcase)
# Define singleton method
obj = Object.new
def obj.special_method
'Special!'
end
# Eigenclass/Singleton class
class << obj
def another_special
'Also special!'
end
end
# ============================================================================
# REFLECTION
# ============================================================================
# Object inspection
'string'.class
'string'.class.superclass
'string'.class.ancestors
'string'.methods
'string'.public_methods
'string'.private_methods
'string'.instance_variables
'string'.instance_of?(String)
'string'.is_a?(Object)
'string'.respond_to?(:upcase)
# Method objects
method_obj = 'string'.method(:upcase)
method_obj.call
method_obj.arity
method_obj.parameters
method_obj.owner
method_obj.source_location rescue nil
# Constants
Object.constants
Math.constants
Math.const_get(:PI)
Math.const_defined?(:PI)
# ============================================================================
# THREADS AND CONCURRENCY
# ============================================================================
# Basic thread
thread = Thread.new do
sleep(1)
'Thread result'
end
thread.join
thread.value
# Multiple threads
threads = []
5.times do |i|
threads << Thread.new do
sleep(rand)
"Thread #{i} finished"
end
end
threads.map(&:join)
# Thread-safe operations
require 'thread'
mutex = Mutex.new
counter = 0
threads = 10.times.map do
Thread.new do
100.times do
mutex.synchronize { counter += 1 }
end
end
end
threads.each(&:join)
# Thread-local variables
Thread.current[:my_var] = 'thread local value'
# ============================================================================
# REGULAR EXPRESSIONS
# ============================================================================
# Basic matching
'Hello World' =~ /World/
/Ruby/.match('I love Ruby')
# Captures
match = /(\w+)@(\w+\.\w+)/.match('user@example.com')
email, domain = match.captures if match
# Named captures
pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
match = pattern.match('2024-03-15')
year = match[:year] if match
# Substitution
replaced = 'Hello World'.gsub(/World/, 'Ruby')
replaced_first = 'aaabbb'.sub(/a/, 'x')
# Scan
matches = 'The number is 42 and 100'.scan(/\d+/)
# Split
parts = 'apple,banana,cherry'.split(/,/)
# ============================================================================
# DATE AND TIME
# ============================================================================
require 'date'
require 'time'
# Time
now = Time.now
specific_time = Time.new(2024, 3, 15, 10, 30, 0)
parsed_time = Time.parse('2024-03-15 10:30:00')
time_utc = Time.now.utc
time_local = Time.now.localtime
# Date
today = Date.today
specific_date = Date.new(2024, 3, 15)
parsed_date = Date.parse('2024-03-15')
tomorrow = today + 1
yesterday = today - 1
# DateTime
require 'date'
datetime = DateTime.now
specific_datetime = DateTime.new(2024, 3, 15, 10, 30, 0)
# Formatting
now.strftime('%Y-%m-%d %H:%M:%S')
now.strftime('%A, %B %d, %Y')
# ============================================================================
# JSON
# ============================================================================
require 'json'
# JSON parsing
json_string = '{"name": "Ruby", "version": 3.0}'
parsed = JSON.parse(json_string)
# JSON generation
data = { name: 'Ruby', version: 3.0 }
json_output = JSON.generate(data)
pretty_json = JSON.pretty_generate(data)
# ============================================================================
# YAML
# ============================================================================
require 'yaml'
# YAML parsing
yaml_string = <<~YAML
name: Ruby
version: 3.0
features:
- Object-oriented
- Dynamic typing
YAML
parsed_yaml = YAML.load(yaml_string)
# YAML generation
yaml_output = { name: 'Ruby', version: 3.0 }.to_yaml
# ============================================================================
# SYMBOLS AND STRINGS
# ============================================================================
# String interpolation
name = 'Ruby'
version = 3.0
interpolated = "#{name} version #{version}"
# String methods
uppercase = 'hello'.upcase
lowercase = 'HELLO'.downcase
capitalized = 'hello'.capitalize
reversed = 'hello'.reverse
stripped = ' hello '.strip
split_string = 'a,b,c'.split(',')
joined = %w[a b c].join('-')
# String inquiry methods
empty_check = ''.empty?
includes = 'hello'.include?('ell')
starts = 'hello'.start_with?('hel')
ends = 'hello'.end_with?('lo')
# Symbol operations
symbol_to_string = :symbol.to_s
string_to_symbol = 'string'.to_sym
symbol_upcase = :hello.upcase
# ============================================================================
# NUMERIC OPERATIONS
# ============================================================================
# Integer methods
absolute = -5.abs
even_check = 4.even?
odd_check = 5.odd?
next_int = 5.next
previous = 5.pred
to_string = 42.to_s
to_float = 42.to_f
times_loop = 3.times.map { |i| i * 2 }
# Float methods
rounded = 3.14159.round(2)
ceiling = 3.14.ceil
floor = 3.99.floor
infinite_check = (1.0 / 0).infinite?
nan_check = (0.0 / 0).nan?
# Numeric enumeration
1.upto(5) { |i| puts i }
5.downto(1) { |i| puts i }
1.step(10, 2) { |i| puts i }
# ============================================================================
# FUNCTIONAL PROGRAMMING CONCEPTS
# ============================================================================
# Function composition
add_one = ->(x) { x + 1 }
multiply_two = ->(x) { x * 2 }
composed = ->(x) { multiply_two.call(add_one.call(x)) }
# Partial application
multiply = ->(x, y) { x * y }
double = multiply.curry[2]
triple = multiply.curry[3]
# Higher-order functions
apply_twice = ->(f, x) { f.call(f.call(x)) }
result = apply_twice.call(add_one, 5)
# Memoization
class Fibonacci
def initialize
@cache = {}
end
def calculate(n)
return n if n <= 1
@cache[n] ||= calculate(n - 1) + calculate(n - 2)
end
end
# ============================================================================
# MONKEY PATCHING (Use with caution!)
# ============================================================================
class String
def palindrome?
self == reverse
end
end
class Integer
def minutes
self * 60
end
def hours
self * 3600
end
end
# ============================================================================
# REFINEMENTS (Safer than monkey patching)
# ============================================================================
module StringExtensions
refine String do
def shout
upcase + '!!!'
end
end
end
class UsingRefinement
using StringExtensions
def demonstrate
'hello'.shout
end
end
# ============================================================================
# OPERATOR OVERLOADING
# ============================================================================
class Vector
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
def +(other)
Vector.new(@x + other.x, @y + other.y)
end
def -(other)
Vector.new(@x - other.x, @y - other.y)
end
def *(scalar)
Vector.new(@x * scalar, @y * scalar)
end
def ==(other)
@x == other.x && @y == other.y
end