Skip to content

Commit

Permalink
Start
Browse files Browse the repository at this point in the history
  • Loading branch information
franzejr committed Oct 31, 2015
0 parents commit 72a75ae
Show file tree
Hide file tree
Showing 38 changed files with 320 additions and 0 deletions.
5 changes: 5 additions & 0 deletions associative_arrays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
aa = [ %w[Someone 1],
%w[Bla 2]]

p aa.assoc("Someone")
p aa.assoc("Bla")
5 changes: 5 additions & 0 deletions autovivification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
deep = Hash.new { |hash,key| hash[key] = Hash.new(&hash.default_proc) }


deep[:a][:b][:c][:d] = 42
p deep
8 changes: 8 additions & 0 deletions blocks_can_take_blocks.rb
Original file line number Diff line number Diff line change
@@ -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 }
9 changes: 9 additions & 0 deletions bubbling_up_thread_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Thread.abort_on_exception = true

Thread.new do
fail 'Ops, we cannot continue'
end

loop do
sleep
end
11 changes: 11 additions & 0 deletions case_on_ranges.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions count_all_objects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'pp'

pp ObjectSpace.count_objects
3 changes: 3 additions & 0 deletions cycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ring = %w[one two three].cycle

p ring.take(5)
5 changes: 5 additions & 0 deletions data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
puts DATA.read

__END__
Hey oh!
Hey oh!
22 changes: 22 additions & 0 deletions easiest_database_pstore.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions easiest_database_pstore_yaml.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions email.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env ruby -w

puts "This is one email"

__END__
11 changes: 11 additions & 0 deletions enable_ruby_warnings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$VERBOSE = true

class WarnMe
def var
@var || 42
end
end


p WarnMe.new.var

6 changes: 6 additions & 0 deletions fast_memoization_fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 8 additions & 0 deletions fetch_data.rb
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 7 additions & 0 deletions get_random_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'securerandom'

p SecureRandom.random_number
p SecureRandom.random_number(100)
p
p SecureRandom.hex(20)
p SecureRandom.base64(20)
2 changes: 2 additions & 0 deletions inject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p (1..10).inject{ |r,e| p [r,e]; r*2}

8 changes: 8 additions & 0 deletions inspecting_the_source_with_script_lines.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions iterating_over_specific_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ObjectSpace.each_object(String) do |object|
p object
end
8 changes: 8 additions & 0 deletions lambda_your_own_syntax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# encoding UTF-8

module Kernel
alias_method , :lambda
end

l = λ { p :called }
l.call
Binary file added mydatabase.pstore
Binary file not shown.
5 changes: 5 additions & 0 deletions people.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
people1: Someone
money1: 400
people2: Someone2
money2: 300
6 changes: 6 additions & 0 deletions print_formatted_with_debug.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def debug(name, content)
p "%s: %p" % [name, content]
end

debug "Num", 42

2 changes: 2 additions & 0 deletions ruby_can_even_explain_it_to_you.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruby -e 'puts { is_this_a_block }' --dump parsetree
ruby -e 'puts { is_this_a_block }' --dump parsetree_with_comment
9 changes: 9 additions & 0 deletions ruby_debug_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def var
@var || 40
end

if $DEBUG
p "var is %p" % var
end

p var + 2
5 changes: 5 additions & 0 deletions shortcut_variable_interpolation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@instance = :instance
@@class = :class
$global = :global

p "#@instance, #@@class, and #$global variables don't need braces"
10 changes: 10 additions & 0 deletions single_instance_running.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions struct_without_assignment.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions super_magic_key_word.rb
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions super_magic_key_word2.rb
Original file line number Diff line number Diff line change
@@ -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 }
15 changes: 15 additions & 0 deletions super_magic_key_word3.rb
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions super_magic_key_word4.rb
Original file line number Diff line number Diff line change
@@ -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 }
15 changes: 15 additions & 0 deletions super_magic_key_word5.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tail_call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true,
trace_instruction: false }

eval <<end
def factorial(n, result=1)
if n==1
result
else
factorial(n-1, n*result)
end
end
end

p factorial(100000)
15 changes: 15 additions & 0 deletions trigger_irb_as_needed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'irb'

def my_program_context
@my_program_context ||= Struct.new(:value).new(40)
end

trap(:INT) do
IRB.start
trap(:INT, 'EXIT')
end

loop do
p "Current value: #{my_program_context.value}"
sleep 1
end
7 changes: 7 additions & 0 deletions turn_on_garbage_collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GC::Profiler.enable

10.times do
array = Array.new(1_000_000) { |i| i.to_s }
end

p GC::Profiler.result
7 changes: 7 additions & 0 deletions unused_variable_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
['Someone', 41, 'another field'],
['Someone2', 42, 'another field2'],
['Someone3', 43, 'another field3']
].each do |name,_,_|
p name
end
3 changes: 3 additions & 0 deletions variables_from_a_regex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if /\A(?<first>\w+),\s*(?<last>\w+)\z/ =~ "Franze, Jr"
puts "#{first} #{last}"
end
6 changes: 6 additions & 0 deletions zip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
letters = "a".."d"
numbers = 1..3

letters.zip(numbers) do |letter, number|
p(letter: letter, number: number)
end

0 comments on commit 72a75ae

Please sign in to comment.