forked from franzejr/best-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 72a75ae
Showing
38 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'pp' | ||
|
||
pp ObjectSpace.count_objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ring = %w[one two three].cycle | ||
|
||
p ring.take(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
puts DATA.read | ||
|
||
__END__ | ||
Hey oh! | ||
Hey oh! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ObjectSpace.each_object(String) do |object| | ||
p object | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
people1: Someone | ||
money1: 400 | ||
people2: Someone2 | ||
money2: 300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |