Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 14:9aa1eba404f0
Add some poking of arrays
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 03 Jan 2017 20:28:17 +0000 |
parents | 3e4ea3d47880 |
children | ebb19742b98f |
files | 1-Ruby/arrays-irb.output 1-Ruby/arrays-ruby.output 1-Ruby/arrays.rb |
diffstat | 3 files changed, 118 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/arrays-irb.output Tue Jan 03 20:28:17 2017 +0000 @@ -0,0 +1,66 @@ +arrays.rb(main):001:0> #! /usr/bin/env ruby +arrays.rb(main):002:0* +arrays.rb(main):003:0* animals = ['lions', 'tigers', 'bears'] +=> ["lions", "tigers", "bears"] +arrays.rb(main):004:0> +arrays.rb(main):005:0* # Default "puts" for array is one element per line +arrays.rb(main):006:0* puts animals +lions +tigers +bears +=> nil +arrays.rb(main):007:0> +arrays.rb(main):008:0* puts "Accessing undefined indexes gives nil" if animals[10] == nil +Accessing undefined indexes gives nil +=> nil +arrays.rb(main):009:0> puts "Negative indexes also work (like Python): -1 = #{animals[-1]}" +Negative indexes also work (like Python): -1 = bears +=> nil +arrays.rb(main):010:0> puts "And you get slices/ranges: 1..2 = #{animals[1..2]}" +And you get slices/ranges: 1..2 = ["tigers", "bears"] +=> nil +arrays.rb(main):011:0> puts "1..2 is even an object: #{(1..2).class}" +1..2 is even an object: Range +=> nil +arrays.rb(main):012:0> puts "and indexers are methods: [1].methods.include?(:[]) = #{[1].methods.include?(:[])}" +and indexers are methods: [1].methods.include?(:[]) = true +=> nil +arrays.rb(main):013:0> # Note: we're using v1.9+ syntax - previously it was "….include?('[]')" +arrays.rb(main):014:0* puts "as is assignment to an array! [1].methods.include?(:[]=) = #{[1].methods.include?(:[]=)}" +as is assignment to an array! [1].methods.include?(:[]=) = true +=> nil +arrays.rb(main):015:0> puts "" + +=> nil +arrays.rb(main):016:0> puts "All funky non-ASCII methods for arrays:" +All funky non-ASCII methods for arrays: +=> nil +arrays.rb(main):017:0> [].methods.each { |method| +arrays.rb(main):018:1* if method !~ /[a-z]/ +arrays.rb(main):019:2> puts method +arrays.rb(main):020:2> end +arrays.rb(main):021:1> } +== +[] +[]= +<< +<=> ++ +* +- +& +| +=== +=~ +!~ +! +!= +=> [: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__] +arrays.rb(main):022:0> +arrays.rb(main):023:0* puts "" + +=> nil +arrays.rb(main):024:0> puts "Arrays are also stacks with push() and pop() methods!" +Arrays are also stacks with push() and pop() methods! +=> nil +arrays.rb(main):025:0>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/arrays-ruby.output Tue Jan 03 20:28:17 2017 +0000 @@ -0,0 +1,28 @@ +lions +tigers +bears +Accessing undefined indexes gives nil +Negative indexes also work (like Python): -1 = bears +And you get slices/ranges: 1..2 = ["tigers", "bears"] +1..2 is even an object: Range +and indexers are methods: [1].methods.include?(:[]) = true +as is assignment to an array! [1].methods.include?(:[]=) = true + +All funky non-ASCII methods for arrays: +== +[] +[]= +<< +<=> ++ +* +- +& +| +=== +=~ +!~ +! +!= + +Arrays are also stacks with push() and pop() methods!
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/arrays.rb Tue Jan 03 20:28:17 2017 +0000 @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby + +animals = ['lions', 'tigers', 'bears'] + +# Default "puts" for array is one element per line +puts animals + +puts "Accessing undefined indexes gives nil" if animals[10] == nil +puts "Negative indexes also work (like Python): -1 = #{animals[-1]}" +puts "And you get slices/ranges: 1..2 = #{animals[1..2]}" +puts "1..2 is even an object: #{(1..2).class}" +puts "and indexers are methods: [1].methods.include?(:[]) = #{[1].methods.include?(:[])}" +# Note: we're using v1.9+ syntax - previously it was "….include?('[]')" +puts "as is assignment to an array! [1].methods.include?(:[]=) = #{[1].methods.include?(:[]=)}" +puts "" +puts "All funky non-ASCII methods for arrays:" +[].methods.each { |method| + if method !~ /[a-z]/ + puts method + end +} + +puts "" +puts "Arrays are also stacks with push() and pop() methods!"