diff --git a/associative_arrays.rb b/associative_arrays.rb new file mode 100644 index 0000000..215cf5b --- /dev/null +++ b/associative_arrays.rb @@ -0,0 +1,5 @@ +aa = [ %w[Someone 1], + %w[Bla 2]] + +p aa.assoc("Someone") +p aa.assoc("Bla") diff --git a/autovivification.rb b/autovivification.rb new file mode 100644 index 0000000..449fbfe --- /dev/null +++ b/autovivification.rb @@ -0,0 +1,5 @@ +deep = Hash.new { |hash,key| hash[key] = Hash.new(&hash.default_proc) } + + +deep[:a][:b][:c][:d] = 42 +p deep diff --git a/blocks_can_take_blocks.rb b/blocks_can_take_blocks.rb new file mode 100644 index 0000000..cad30bb --- /dev/null +++ b/blocks_can_take_blocks.rb @@ -0,0 +1,8 @@ +var = :var +object = Object.new + +object.define_singleton_method(:show_var_and_block) do |&block| + p [var, block] +end + +object.show_var_and_block { :block } diff --git a/bubbling_up_thread_errors.rb b/bubbling_up_thread_errors.rb new file mode 100644 index 0000000..a80ee7e --- /dev/null +++ b/bubbling_up_thread_errors.rb @@ -0,0 +1,9 @@ +Thread.abort_on_exception = true + +Thread.new do + fail 'Ops, we cannot continue' +end + +loop do + sleep +end diff --git a/case_on_ranges.rb b/case_on_ranges.rb new file mode 100644 index 0000000..fe0eb4e --- /dev/null +++ b/case_on_ranges.rb @@ -0,0 +1,11 @@ +age = rand(1..100) +p age + +case age + when -Float::INFINITY..20 + p 'You are too young' + when 21..64 + p 'You are at the right age' + when 65..Float::INFINITY + p 'You are too old' +end diff --git a/count_all_objects.rb b/count_all_objects.rb new file mode 100644 index 0000000..9884c05 --- /dev/null +++ b/count_all_objects.rb @@ -0,0 +1,3 @@ +require 'pp' + +pp ObjectSpace.count_objects diff --git a/cycle.rb b/cycle.rb new file mode 100644 index 0000000..21a5605 --- /dev/null +++ b/cycle.rb @@ -0,0 +1,3 @@ +ring = %w[one two three].cycle + +p ring.take(5) diff --git a/data.rb b/data.rb new file mode 100644 index 0000000..f9ed72b --- /dev/null +++ b/data.rb @@ -0,0 +1,5 @@ +puts DATA.read + +__END__ +Hey oh! +Hey oh! diff --git a/easiest_database_pstore.rb b/easiest_database_pstore.rb new file mode 100644 index 0000000..2f7d2cc --- /dev/null +++ b/easiest_database_pstore.rb @@ -0,0 +1,22 @@ +require 'pstore' + +db = PStore.new('mydatabase.pstore') + +db.transaction do + db['people1'] = 'Someone' + db['money1'] = 400 +end + +db.transaction do + db['people2'] = 'Someone2' + db['money2'] = 300 +end + + +db.transaction(true) do + p 'People %p' % db['people1'] + p 'Money %p' % db['money1'] + p "SECOND PERSON" + p 'People %p' % db['people2'] + p 'Money %p' % db['money2'] +end diff --git a/easiest_database_pstore_yaml.rb b/easiest_database_pstore_yaml.rb new file mode 100644 index 0000000..8dab8a2 --- /dev/null +++ b/easiest_database_pstore_yaml.rb @@ -0,0 +1,22 @@ +require 'yaml/store' + +db = YAML::Store.new('people.yml') + +db.transaction do + db['people1'] = 'Someone' + db['money1'] = 400 +end + +db.transaction do + db['people2'] = 'Someone2' + db['money2'] = 300 +end + + +db.transaction(true) do + p 'People %p' % db['people1'] + p 'Money %p' % db['money1'] + p "SECOND PERSON" + p 'People %p' % db['people2'] + p 'Money %p' % db['money2'] +end diff --git a/email.txt b/email.txt new file mode 100644 index 0000000..0cf735b --- /dev/null +++ b/email.txt @@ -0,0 +1,5 @@ +#! /usr/bin/env ruby -w + +puts "This is one email" + +__END__ diff --git a/enable_ruby_warnings.rb b/enable_ruby_warnings.rb new file mode 100644 index 0000000..8dcfd42 --- /dev/null +++ b/enable_ruby_warnings.rb @@ -0,0 +1,11 @@ +$VERBOSE = true + +class WarnMe + def var + @var || 42 + end +end + + +p WarnMe.new.var + diff --git a/fast_memoization_fibonacci.rb b/fast_memoization_fibonacci.rb new file mode 100644 index 0000000..8fe5e9b --- /dev/null +++ b/fast_memoization_fibonacci.rb @@ -0,0 +1,6 @@ +fibonacci = Hash.new{ |numbers,index| + numbers[index] = fibonacci[index - 2] + fibonacci[index - 1] +}.update(0 => 0, 1 => 1) + + +p fibonacci[300] diff --git a/fetch_data.rb b/fetch_data.rb new file mode 100644 index 0000000..c86ff24 --- /dev/null +++ b/fetch_data.rb @@ -0,0 +1,8 @@ +params = {var: 42} + +p params.fetch(:var) +p params.fetch(:missing, 42) +p params.fetch(:missing) { 40 + 2 } + +params.fetch(:missing) + diff --git a/get_random_data.rb b/get_random_data.rb new file mode 100644 index 0000000..46377b1 --- /dev/null +++ b/get_random_data.rb @@ -0,0 +1,7 @@ +require 'securerandom' + +p SecureRandom.random_number +p SecureRandom.random_number(100) +p +p SecureRandom.hex(20) +p SecureRandom.base64(20) diff --git a/inject.rb b/inject.rb new file mode 100644 index 0000000..fd21b34 --- /dev/null +++ b/inject.rb @@ -0,0 +1,2 @@ +p (1..10).inject{ |r,e| p [r,e]; r*2} + diff --git a/inspecting_the_source_with_script_lines.rb b/inspecting_the_source_with_script_lines.rb new file mode 100644 index 0000000..e119076 --- /dev/null +++ b/inspecting_the_source_with_script_lines.rb @@ -0,0 +1,8 @@ +SCRIPT_LINES__ = { } + +#require_relative = 'better_be_well_formed_code' +require_relative = 'better_be_well_formed_code_with_a_line_size_greather_than_80_it_is_not_good' + +if SCRIPT_LINES__.values.flatten.any? { |line| line.size > 80} + abort 'Clean up your code first!' +end diff --git a/iterating_over_specific_types.rb b/iterating_over_specific_types.rb new file mode 100644 index 0000000..baa046e --- /dev/null +++ b/iterating_over_specific_types.rb @@ -0,0 +1,3 @@ +ObjectSpace.each_object(String) do |object| + p object +end diff --git a/lambda_your_own_syntax.rb b/lambda_your_own_syntax.rb new file mode 100644 index 0000000..a541bcc --- /dev/null +++ b/lambda_your_own_syntax.rb @@ -0,0 +1,8 @@ +# encoding UTF-8 + +module Kernel + alias_method :λ, :lambda +end + +l = λ { p :called } +l.call diff --git a/mydatabase.pstore b/mydatabase.pstore new file mode 100644 index 0000000..04203fa Binary files /dev/null and b/mydatabase.pstore differ diff --git a/people.yml b/people.yml new file mode 100644 index 0000000..ee7a285 --- /dev/null +++ b/people.yml @@ -0,0 +1,5 @@ +--- +people1: Someone +money1: 400 +people2: Someone2 +money2: 300 diff --git a/print_formatted_with_debug.rb b/print_formatted_with_debug.rb new file mode 100644 index 0000000..00cdba4 --- /dev/null +++ b/print_formatted_with_debug.rb @@ -0,0 +1,6 @@ +def debug(name, content) + p "%s: %p" % [name, content] +end + +debug "Num", 42 + diff --git a/ruby_can_even_explain_it_to_you.txt b/ruby_can_even_explain_it_to_you.txt new file mode 100644 index 0000000..4349c5d --- /dev/null +++ b/ruby_can_even_explain_it_to_you.txt @@ -0,0 +1,2 @@ +ruby -e 'puts { is_this_a_block }' --dump parsetree +ruby -e 'puts { is_this_a_block }' --dump parsetree_with_comment diff --git a/ruby_debug_flag.rb b/ruby_debug_flag.rb new file mode 100644 index 0000000..c059f9d --- /dev/null +++ b/ruby_debug_flag.rb @@ -0,0 +1,9 @@ +def var + @var || 40 +end + +if $DEBUG + p "var is %p" % var +end + +p var + 2 diff --git a/shortcut_variable_interpolation.rb b/shortcut_variable_interpolation.rb new file mode 100644 index 0000000..46999f2 --- /dev/null +++ b/shortcut_variable_interpolation.rb @@ -0,0 +1,5 @@ +@instance = :instance +@@class = :class +$global = :global + +p "#@instance, #@@class, and #$global variables don't need braces" diff --git a/single_instance_running.rb b/single_instance_running.rb new file mode 100644 index 0000000..c75ed86 --- /dev/null +++ b/single_instance_running.rb @@ -0,0 +1,10 @@ +DATA.flock(File::LOCK_EX | File::LOCK_NB) or abort 'Already running' + +trap('INT', 'EXIT') +puts 'Running...' +loop do + sleep +end + +__END__ +DO NOT DELETE: used for locking diff --git a/struct_without_assignment.rb b/struct_without_assignment.rb new file mode 100644 index 0000000..68ccbec --- /dev/null +++ b/struct_without_assignment.rb @@ -0,0 +1,8 @@ +Struct.new("Name", :first, :last) do + def full + "#{first} #{last}" + end +end + +franzejr = Struct::Name.new("Franze", "Jr") +p franzejr.full diff --git a/super_magic_key_word.rb b/super_magic_key_word.rb new file mode 100644 index 0000000..a05372d --- /dev/null +++ b/super_magic_key_word.rb @@ -0,0 +1,13 @@ +class Parent + def show_args(*args) + p args + end +end + +class Child < Parent + def show_args(a,b,c) + super(a,b,c) + end +end + +Child.new.show_args(:a, :b, :c) diff --git a/super_magic_key_word2.rb b/super_magic_key_word2.rb new file mode 100644 index 0000000..d12674d --- /dev/null +++ b/super_magic_key_word2.rb @@ -0,0 +1,14 @@ +class Parent + def show_args(*args, &block) + p [*args, block] + end +end + +class Child < Parent + def show_args(a,b,c) + super + end +end + +#Everything goes up, including the block +Child.new.show_args(:a, :b, :c) { :block } diff --git a/super_magic_key_word3.rb b/super_magic_key_word3.rb new file mode 100644 index 0000000..453a5bb --- /dev/null +++ b/super_magic_key_word3.rb @@ -0,0 +1,15 @@ +class Parent + def show_args(*args, &block) + p [*args, block] + end +end + +class Child < Parent + def show_args(a,b,c) + #modify super by passing nothing + super() + end +end + +#Nothing goes up +Child.new.show_args(:a, :b, :c) diff --git a/super_magic_key_word4.rb b/super_magic_key_word4.rb new file mode 100644 index 0000000..a2160d1 --- /dev/null +++ b/super_magic_key_word4.rb @@ -0,0 +1,15 @@ +class Parent + def show_args(*args, &block) + p [*args, block] + end +end + +class Child < Parent + def show_args(a,b,c) + #modify super by passing nothing + super(&nil) + end +end + +#Nothing goes up, neither the block +Child.new.show_args(:a, :b, :c) { :block } diff --git a/super_magic_key_word5.rb b/super_magic_key_word5.rb new file mode 100644 index 0000000..b24abf0 --- /dev/null +++ b/super_magic_key_word5.rb @@ -0,0 +1,15 @@ +class DontDelegateToMe; end +class DelegateToMe; def delegate; "DelegateToMe" end end + +module DelegateIfCan + def delegate + if defined? super + "Modified: #{super}" + else + "DelegateIfCan" + end + end +end + +p DelegateToMe.new.extend(DelegateIfCan).delegate +p DontDelegateToMe.new.extend(DelegateIfCan).delegate diff --git a/tail_call.rb b/tail_call.rb new file mode 100644 index 0000000..842fd4d --- /dev/null +++ b/tail_call.rb @@ -0,0 +1,14 @@ +RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, + trace_instruction: false } + +eval <\w+),\s*(?\w+)\z/ =~ "Franze, Jr" + puts "#{first} #{last}" +end diff --git a/zip.rb b/zip.rb new file mode 100644 index 0000000..a71a00d --- /dev/null +++ b/zip.rb @@ -0,0 +1,6 @@ +letters = "a".."d" +numbers = 1..3 + +letters.zip(numbers) do |letter, number| + p(letter: letter, number: number) +end