Skip to content

Commit

Permalink
Add expected outputs when running files.
Browse files Browse the repository at this point in the history
I used following command to help me so I didn't have to run each file
manually and append the append to the source:

```
find . -iname '*.rb' -exec sh -c 'echo "\n# Result:" >> {}; gtimeout 2 ruby {} 2>&1 | sed "s/^/# /" >> {}' \;
```
  • Loading branch information
rafaelsales committed Nov 1, 2015
1 parent 3e80f98 commit 2c2ef46
Show file tree
Hide file tree
Showing 30 changed files with 147 additions and 0 deletions.
4 changes: 4 additions & 0 deletions associative_arrays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

p aa.assoc("Someone")
p aa.assoc("Bla")

# Result:
# ["Someone", "1"]
# ["Bla", "2"]
3 changes: 3 additions & 0 deletions autovivification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

deep[:a][:b][:c][:d] = 42
p deep

# Result:
# {:a=>{:b=>{:c=>{:d=>42}}}}
3 changes: 3 additions & 0 deletions blocks_can_take_blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
end

object.show_var_and_block { :block }

# Result:
# [:var, #<Proc:0x007ffd6c038128@./blocks_can_take_blocks.rb:8>]
3 changes: 3 additions & 0 deletions bubbling_up_thread_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
loop do
sleep
end

# Result:
# ./bubbling_up_thread_errors.rb:4:in `block in <main>': Ops, we cannot continue (RuntimeError)
4 changes: 4 additions & 0 deletions case_on_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
when 65..Float::INFINITY
p 'You are too old'
end

# Result:
# 55
# "You are at the right age"
20 changes: 20 additions & 0 deletions count_all_objects.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
require 'pp'

pp ObjectSpace.count_objects

# Result:
# {:TOTAL=>30163,
# :FREE=>1007,
# :T_OBJECT=>39,
# :T_CLASS=>534,
# :T_MODULE=>24,
# :T_FLOAT=>4,
# :T_STRING=>9290,
# :T_REGEXP=>70,
# :T_ARRAY=>2231,
# :T_HASH=>53,
# :T_STRUCT=>1,
# :T_BIGNUM=>2,
# :T_FILE=>14,
# :T_DATA=>966,
# :T_MATCH=>1,
# :T_COMPLEX=>1,
# :T_NODE=>15896,
# :T_ICLASS=>30}
3 changes: 3 additions & 0 deletions cycle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ring = %w[one two three].cycle

p ring.take(5)

# Result:
# ["one", "two", "three", "one", "two"]
7 changes: 7 additions & 0 deletions easiest_database_pstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@
p 'People %p' % db['people2']
p 'Money %p' % db['money2']
end

# Result:
# "People \"Someone\""
# "Money 400"
# "SECOND PERSON"
# "People \"Someone2\""
# "Money 300"
7 changes: 7 additions & 0 deletions easiest_database_pstore_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@
p 'People %p' % db['people2']
p 'Money %p' % db['money2']
end

# Result:
# "People \"Someone\""
# "Money 400"
# "SECOND PERSON"
# "People \"Someone2\""
# "Money 300"
4 changes: 4 additions & 0 deletions enable_ruby_warnings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ def var

p WarnMe.new.var


# Result:
# ./enable_ruby_warnings.rb:5: warning: instance variable @var not initialized
# 42
3 changes: 3 additions & 0 deletions fast_memoization_fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@


p fibonacci[300]

# Result:
# 222232244629420445529739893461909967206666939096499764990979600
7 changes: 7 additions & 0 deletions fetch_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@

params.fetch(:missing)


# Result:
# 42
# 42
# 42
# ./fetch_data.rb:7:in `fetch': key not found: :missing (KeyError)
# from ./fetch_data.rb:7:in `<main>'
6 changes: 6 additions & 0 deletions get_random_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
p
p SecureRandom.hex(20)
p SecureRandom.base64(20)

# Result:
# 0.7851536586163714
# 46
# "3efb674fbc2ba390856c15489652e75e8afff6d1"
# "yFv0WzugzFC6/D71teVe1Y5r1kU="
12 changes: 12 additions & 0 deletions inject.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
p (1..10).inject{ |r,e| p [r,e]; r*2}


# Result:
# [1, 2]
# [2, 3]
# [4, 4]
# [8, 5]
# [16, 6]
# [32, 7]
# [64, 8]
# [128, 9]
# [256, 10]
# 512
8 changes: 8 additions & 0 deletions iterating_over_specific_types.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ObjectSpace.each_object(String) do |object|
p object
end

# Result:
# "block in dependent_specs"
# "block in dependent_specs"
# "block (3 levels) in dependent_gems"
# "block (3 levels) in dependent_gems"
# ... (huge output suppressed)
# "This rdoc is bundled with Ruby"
3 changes: 3 additions & 0 deletions lambda_your_own_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ module Kernel

l = λ { p :called }
l.call

# Result:
# :called
2 changes: 2 additions & 0 deletions print_formatted_with_debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ def debug(name, content)

debug "Num", 42

# Result:
# "Num: 42"
6 changes: 6 additions & 0 deletions ruby_debug_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ def var
end

p var + 2

# Result:
# ruby_debug_flag.rb:2: warning: instance variable @var not initialized
# "var is 40"
# ruby_debug_flag.rb:2: warning: instance variable @var not initialized
# 42
3 changes: 3 additions & 0 deletions shortcut_variable_interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
$global = :global

p "#@instance, #@@class, and #$global variables don't need braces"

# Result:
# "instance, class, and global variables don't need braces"
3 changes: 3 additions & 0 deletions struct_without_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ def full

franzejr = Struct::Name.new("Franze", "Jr")
p franzejr.full

# Result:
# "Franze Jr"
3 changes: 3 additions & 0 deletions super_magic_key_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ def show_args(a,b,c)
end

Child.new.show_args(:a, :b, :c)

# Result:
# [:a, :b, :c]
3 changes: 3 additions & 0 deletions super_magic_key_word2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ def show_args(a,b,c)

#Everything goes up, including the block
Child.new.show_args(:a, :b, :c) { :block }

# Result:
# [:a, :b, :c, #<Proc:0x007f9bd288bfb0@./super_magic_key_word2.rb:14>]
3 changes: 3 additions & 0 deletions super_magic_key_word3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def show_args(a,b,c)

#Nothing goes up
Child.new.show_args(:a, :b, :c)

# Result:
# [nil]
3 changes: 3 additions & 0 deletions super_magic_key_word4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def show_args(a,b,c)

#Nothing goes up, neither the block
Child.new.show_args(:a, :b, :c) { :block }

# Result:
# [nil]
4 changes: 4 additions & 0 deletions super_magic_key_word5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def delegate

p DelegateToMe.new.extend(DelegateIfCan).delegate
p DontDelegateToMe.new.extend(DelegateIfCan).delegate

# Result:
# "Modified: DelegateToMe"
# "DelegateIfCan"
2 changes: 2 additions & 0 deletions tail_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ def factorial(n, result=1)
end

p factorial(100000)

# Result:
4 changes: 4 additions & 0 deletions trigger_irb_as_needed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def my_program_context
p "Current value: #{my_program_context.value}"
sleep 1
end

# Result:
# "Current value: 40"
# "Current value: 40"
5 changes: 5 additions & 0 deletions unused_variable_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
].each do |name,_,_|
p name
end

# Result:
# "Someone"
# "Someone2"
# "Someone3"
3 changes: 3 additions & 0 deletions variables_from_a_regex.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
if /\A(?<first>\w+),\s*(?<last>\w+)\z/ =~ "Franze, Jr"
puts "#{first} #{last}"
end

# Result:
# Franze Jr
6 changes: 6 additions & 0 deletions zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
letters.zip(numbers) do |letter, number|
p(letter: letter, number: number)
end

# Result:
# {:letter=>"a", :number=>1}
# {:letter=>"b", :number=>2}
# {:letter=>"c", :number=>3}
# {:letter=>"d", :number=>nil}

0 comments on commit 2c2ef46

Please sign in to comment.