Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 1-Ruby/arrays-irb.output @ 21:7e212cc7ec24
Add default Tree code from book's source code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 07 Jan 2017 20:36:33 +0000 |
parents | 8d46064c9afc |
children |
line wrap: on
line source
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 do |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> end == [] []= << <=> + * - & | === =~ !~ ! != => [: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>