Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/arrays.rb @ 25:e26607247dd2
Add sample data from book
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 20 Jan 2017 21:01:09 +0000 |
parents | 8d46064c9afc |
children |
rev | line source |
---|---|
14 | 1 #! /usr/bin/env ruby |
2 | |
3 animals = ['lions', 'tigers', 'bears'] | |
4 | |
5 # Default "puts" for array is one element per line | |
6 puts animals | |
7 | |
8 puts "Accessing undefined indexes gives nil" if animals[10] == nil | |
9 puts "Negative indexes also work (like Python): -1 = #{animals[-1]}" | |
10 puts "And you get slices/ranges: 1..2 = #{animals[1..2]}" | |
11 puts "1..2 is even an object: #{(1..2).class}" | |
12 puts "and indexers are methods: [1].methods.include?(:[]) = #{[1].methods.include?(:[])}" | |
13 # Note: we're using v1.9+ syntax - previously it was "….include?('[]')" | |
14 puts "as is assignment to an array! [1].methods.include?(:[]=) = #{[1].methods.include?(:[]=)}" | |
15 puts "" | |
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 | 18 if method !~ /[a-z]/ |
19 puts method | |
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 | 22 |
23 puts "" | |
24 puts "Arrays are also stacks with push() and pop() methods!" |