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