Skip to content

Commit 72a75ae

Browse files
committed
Start
0 parents  commit 72a75ae

38 files changed

+320
-0
lines changed

associative_arrays.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
aa = [ %w[Someone 1],
2+
%w[Bla 2]]
3+
4+
p aa.assoc("Someone")
5+
p aa.assoc("Bla")

autovivification.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
deep = Hash.new { |hash,key| hash[key] = Hash.new(&hash.default_proc) }
2+
3+
4+
deep[:a][:b][:c][:d] = 42
5+
p deep

blocks_can_take_blocks.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var = :var
2+
object = Object.new
3+
4+
object.define_singleton_method(:show_var_and_block) do |&block|
5+
p [var, block]
6+
end
7+
8+
object.show_var_and_block { :block }

bubbling_up_thread_errors.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Thread.abort_on_exception = true
2+
3+
Thread.new do
4+
fail 'Ops, we cannot continue'
5+
end
6+
7+
loop do
8+
sleep
9+
end

case_on_ranges.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
age = rand(1..100)
2+
p age
3+
4+
case age
5+
when -Float::INFINITY..20
6+
p 'You are too young'
7+
when 21..64
8+
p 'You are at the right age'
9+
when 65..Float::INFINITY
10+
p 'You are too old'
11+
end

count_all_objects.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'pp'
2+
3+
pp ObjectSpace.count_objects

cycle.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ring = %w[one two three].cycle
2+
3+
p ring.take(5)

data.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
puts DATA.read
2+
3+
__END__
4+
Hey oh!
5+
Hey oh!

easiest_database_pstore.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'pstore'
2+
3+
db = PStore.new('mydatabase.pstore')
4+
5+
db.transaction do
6+
db['people1'] = 'Someone'
7+
db['money1'] = 400
8+
end
9+
10+
db.transaction do
11+
db['people2'] = 'Someone2'
12+
db['money2'] = 300
13+
end
14+
15+
16+
db.transaction(true) do
17+
p 'People %p' % db['people1']
18+
p 'Money %p' % db['money1']
19+
p "SECOND PERSON"
20+
p 'People %p' % db['people2']
21+
p 'Money %p' % db['money2']
22+
end

easiest_database_pstore_yaml.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'yaml/store'
2+
3+
db = YAML::Store.new('people.yml')
4+
5+
db.transaction do
6+
db['people1'] = 'Someone'
7+
db['money1'] = 400
8+
end
9+
10+
db.transaction do
11+
db['people2'] = 'Someone2'
12+
db['money2'] = 300
13+
end
14+
15+
16+
db.transaction(true) do
17+
p 'People %p' % db['people1']
18+
p 'Money %p' % db['money1']
19+
p "SECOND PERSON"
20+
p 'People %p' % db['people2']
21+
p 'Money %p' % db['money2']
22+
end

0 commit comments

Comments
 (0)