Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/arrays-irb.output @ 29:9c7af76fdbd0
Print isn't PrinLn, so add some \n characters to the output
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 06 Sep 2017 18:36:02 +0100 |
parents | 8d46064c9afc |
children |
rev | line source |
---|---|
14 | 1 arrays.rb(main):001:0> #! /usr/bin/env ruby |
2 arrays.rb(main):002:0* | |
3 arrays.rb(main):003:0* animals = ['lions', 'tigers', 'bears'] | |
4 => ["lions", "tigers", "bears"] | |
5 arrays.rb(main):004:0> | |
6 arrays.rb(main):005:0* # Default "puts" for array is one element per line | |
7 arrays.rb(main):006:0* puts animals | |
8 lions | |
9 tigers | |
10 bears | |
11 => nil | |
12 arrays.rb(main):007:0> | |
13 arrays.rb(main):008:0* puts "Accessing undefined indexes gives nil" if animals[10] == nil | |
14 Accessing undefined indexes gives nil | |
15 => nil | |
16 arrays.rb(main):009:0> puts "Negative indexes also work (like Python): -1 = #{animals[-1]}" | |
17 Negative indexes also work (like Python): -1 = bears | |
18 => nil | |
19 arrays.rb(main):010:0> puts "And you get slices/ranges: 1..2 = #{animals[1..2]}" | |
20 And you get slices/ranges: 1..2 = ["tigers", "bears"] | |
21 => nil | |
22 arrays.rb(main):011:0> puts "1..2 is even an object: #{(1..2).class}" | |
23 1..2 is even an object: Range | |
24 => nil | |
25 arrays.rb(main):012:0> puts "and indexers are methods: [1].methods.include?(:[]) = #{[1].methods.include?(:[])}" | |
26 and indexers are methods: [1].methods.include?(:[]) = true | |
27 => nil | |
28 arrays.rb(main):013:0> # Note: we're using v1.9+ syntax - previously it was "….include?('[]')" | |
29 arrays.rb(main):014:0* puts "as is assignment to an array! [1].methods.include?(:[]=) = #{[1].methods.include?(:[]=)}" | |
30 as is assignment to an array! [1].methods.include?(:[]=) = true | |
31 => nil | |
32 arrays.rb(main):015:0> puts "" | |
33 | |
34 => nil | |
35 arrays.rb(main):016:0> puts "All funky non-ASCII methods for arrays:" | |
36 All funky non-ASCII methods for arrays: | |
37 => nil | |
16
8d46064c9afc
Be a nice Ruby user and follow block convention for do/end vs {}
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
38 arrays.rb(main):017:0> [].methods.each do |method| |
14 | 39 arrays.rb(main):018:1* if method !~ /[a-z]/ |
40 arrays.rb(main):019:2> puts method | |
41 arrays.rb(main):020:2> 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
|
42 arrays.rb(main):021:1> end |
14 | 43 == |
44 [] | |
45 []= | |
46 << | |
47 <=> | |
48 + | |
49 * | |
50 - | |
51 & | |
52 | | |
53 === | |
54 =~ | |
55 !~ | |
56 ! | |
57 != | |
58 => [:inspect, :to_s, :to_a, :to_h, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :bsearch, :pack, :entries, :sort_by, :grep, :find, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :any?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :lazy, :nil?, :===, :=~, :!~, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] | |
59 arrays.rb(main):022:0> | |
60 arrays.rb(main):023:0* puts "" | |
61 | |
62 => nil | |
63 arrays.rb(main):024:0> puts "Arrays are also stacks with push() and pop() methods!" | |
64 Arrays are also stacks with push() and pop() methods! | |
65 => nil | |
66 arrays.rb(main):025:0> |