annotate 1-Ruby/code-blocks.rb @ 17:61dfac9a058a

Add some initial experiments with blocks
author IBBoard <dev@ibboard.co.uk>
date Tue, 03 Jan 2017 20:58:19 +0000
parents
children e58952c15e5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #! /usr/bin/env ruby
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # Simple passing of code block
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 3.times { puts 'Repeated string' }
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 # Extending a class
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 class Fixnum
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 def my_times
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 # 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
10 i = self
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 until i == 0
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 i = i - 1
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 # Magic happens here!
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 # 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
15 yield
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 3.my_times { puts 'My repeated string' }
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 # 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
23 my_block = proc { puts 'I am a block' }
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 puts my_block.class
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 # 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
27 def call_block(&block)
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 block.call
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 def pass_block(&block)
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 call_block(&block)
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 pass_block { puts 'Inline block' }
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 pass_block &my_block
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 # 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
37 # 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
38 # 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
39 # I tried. Calling a block definitely needs ".call"
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 #
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 # 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
42 # 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
43 # That means that this works:
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 def pass_var_block(block)
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 call_block(&block)
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 end
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47
61dfac9a058a Add some initial experiments with blocks
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 pass_var_block my_block