Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/code-blocks-irb.output @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 61dfac9a058a |
children |
rev | line source |
---|---|
17
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 code-blocks.rb(main):001:0> #! /usr/bin/env ruby |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 code-blocks.rb(main):002:0* |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 code-blocks.rb(main):003:0* # Simple passing of code block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 code-blocks.rb(main):004:0* 3.times { puts 'Repeated string' } |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 Repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 Repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 Repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 => 3 |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 code-blocks.rb(main):005:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 code-blocks.rb(main):006:0* # Extending a class |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 code-blocks.rb(main):007:0* class Fixnum |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 code-blocks.rb(main):008:1> def my_times |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 code-blocks.rb(main):009:2> # We're in a number, so store it as an iterator |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 code-blocks.rb(main):010:2* i = self |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 code-blocks.rb(main):011:2> until i == 0 |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 code-blocks.rb(main):012:3> i = i - 1 |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 code-blocks.rb(main):013:3> # Magic happens here! |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 code-blocks.rb(main):014:3* # Yield normally yields a value, but somehow we're executing a blockā¦ |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 code-blocks.rb(main):015:3* yield |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 code-blocks.rb(main):016:3> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 code-blocks.rb(main):017:2> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 code-blocks.rb(main):018:1> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 => :my_times |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 code-blocks.rb(main):019:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 code-blocks.rb(main):020:0* 3.my_times { puts 'My repeated string' } |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 My repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 My repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 My repeated string |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 => nil |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 code-blocks.rb(main):021:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 code-blocks.rb(main):022:0* # Blocks are objects, if you do it right - https://www.ruby-forum.com/topic/49018 |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 code-blocks.rb(main):023:0* my_block = proc { puts 'I am a block' } |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 => #<Proc:0x000000017361e8@code-blocks.rb:23> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 code-blocks.rb(main):024:0> puts my_block.class |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 Proc |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 => nil |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 code-blocks.rb(main):025:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 code-blocks.rb(main):026:0* # Blocks can be passed around - "&var" says "this is a block" |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 code-blocks.rb(main):027:0* def call_block(&block) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 code-blocks.rb(main):028:1> block.call |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 code-blocks.rb(main):029:1> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 => :call_block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 code-blocks.rb(main):030:0> def pass_block(&block) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 code-blocks.rb(main):031:1> call_block(&block) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 code-blocks.rb(main):032:1> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 => :pass_block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 code-blocks.rb(main):033:0> pass_block { puts 'Inline block' } |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 Inline block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 => nil |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 code-blocks.rb(main):034:0> pass_block &my_block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 I am a block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 => nil |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 code-blocks.rb(main):035:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 code-blocks.rb(main):036:0* # Without the "&" we get "wrong number of arguments (1 for 0)" because |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 code-blocks.rb(main):037:0* # a block and an argument are different somehow. This is *not* |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 code-blocks.rb(main):038:0* # because we were trying to call "my_block" and pass what it returned (nil). |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 code-blocks.rb(main):039:0* # I tried. Calling a block definitely needs ".call" |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 code-blocks.rb(main):040:0* # |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 code-blocks.rb(main):041:0* # This page (https://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 code-blocks.rb(main):042:0* # makes it cleared: ANY method can take a block independent of its arguments. |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 code-blocks.rb(main):043:0* # That means that this works: |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 code-blocks.rb(main):044:0* def pass_var_block(block) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 code-blocks.rb(main):045:1> call_block(&block) |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 code-blocks.rb(main):046:1> end |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 => :pass_var_block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 code-blocks.rb(main):047:0> |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 code-blocks.rb(main):048:0* pass_var_block my_block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 I am a block |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 => nil |
61dfac9a058a
Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 code-blocks.rb(main):049:0> |