annotate 1-Ruby/arrays.rb @ 16:8d46064c9afc

Be a nice Ruby user and follow block convention for do/end vs {}
author IBBoard <dev@ibboard.co.uk>
date Tue, 03 Jan 2017 20:38:45 +0000
parents 9aa1eba404f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #! /usr/bin/env ruby
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 animals = ['lions', 'tigers', 'bears']
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # Default "puts" for array is one element per line
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 puts animals
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 puts "Accessing undefined indexes gives nil" if animals[10] == nil
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 puts "Negative indexes also work (like Python): -1 = #{animals[-1]}"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 puts "And you get slices/ranges: 1..2 = #{animals[1..2]}"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 puts "1..2 is even an object: #{(1..2).class}"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 puts "and indexers are methods: [1].methods.include?(:[]) = #{[1].methods.include?(:[])}"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 # Note: we're using v1.9+ syntax - previously it was "….include?('[]')"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 puts "as is assignment to an array! [1].methods.include?(:[]=) = #{[1].methods.include?(:[]=)}"
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 puts ""
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 puts "All funky non-ASCII methods for arrays:"
16
8d46064c9afc Be a nice Ruby user and follow block convention for do/end vs {}
IBBoard <dev@ibboard.co.uk>
parents: 14
diff changeset
17 [].methods.each do |method|
14
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 if method !~ /[a-z]/
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 puts method
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 end
16
8d46064c9afc Be a nice Ruby user and follow block convention for do/end vs {}
IBBoard <dev@ibboard.co.uk>
parents: 14
diff changeset
21 end
14
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 puts ""
9aa1eba404f0 Add some poking of arrays
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 puts "Arrays are also stacks with push() and pop() methods!"