Script started on Mon May 8 10:28:55 2023 bash-3.2$ irb irb(main):001:0> nums = [1,2,3,4,5,6,7,8,9] => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):002:0> nums.each { "it's a number" } => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):003:0> nums => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):004:0> nums.each { puts "it's a number" } it's a number it's a number it's a number it's a number it's a number it's a number it's a number it's a number it's a number => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):005:0> nums.each { | n | puts n } 1 2 3 4 5 6 7 8 9 => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):006:0> sum = 0 => 0 irb(main):007:0> nums.each { | n | sum += n } => [1, 2, 3, 4, 5, 6, 7, 8, 9] irb(main):008:0> sum => 45 irb(main):009:0> 5.times { | n | puts n } 0 1 2 3 4 => 5 irb(main):011:0> things = { "this" => 17, "that" => "hello", "nogots" => nil } => {"this"=>17, "that"=>"hello", "nogots"=>nil} irb(main):012:0> things.each { puts "it's a thing" } it's a thing it's a thing it's a thing => {"this"=>17, "that"=>"hello", "nogots"=>nil} irb(main):014:0> things.each { |k, v| puts "key = " + k + " value = " + v } TypeError: no implicit conversion of Fixnum into String from (irb):14:in `+' from (irb):14:in `block in irb_binding' from (irb):14:in `each' from (irb):14 from /usr/bin/irb:11:in `
' irb(main):015:0> things => {"this"=>17, "that"=>"hello", "nogots"=>nil} irb(main):016:0> things.each { |k, v| puts "key = " + k.to_s + " value = " + v.to_s } key = this value = 17 key = that value = hello key = nogots value = => {"this"=>17, "that"=>"hello", "nogots"=>nil} irb(main):017:0> things.methods => [:<, :>, :<=, :>=, :==, :[], :[]=, :empty?, :eql?, :inspect, :length, :size, :each, :to_hash, :to_proc, :to_a, :to_s, :dig, :hash, :to_h, :include?, :select, :reject, :any?, :member?, :index, :replace, :clear, :delete, :fetch, :shift, :select!, :keep_if, :values_at, :delete_if, :reject!, :assoc, :rassoc, :flatten, :default, :rehash, :store, :default=, :default_proc, :default_proc=, :key, :each_value, :each_key, :each_pair, :keys, :values, :fetch_values, :invert, :update, :merge!, :merge, :has_key?, :has_value?, :key?, :value?, :compare_by_identity, :compare_by_identity?, :find, :entries, :sort, :sort_by, :grep, :grep_v, :count, :detect, :find_index, :find_all, :collect, :map, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :first, :all?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :each_with_index, :reverse_each, :each_entry, :each_slice, :each_cons, :each_with_object, :zip, :take, :take_while, :drop, :drop_while, :cycle, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :lazy, :tap, :public_send, :instance_variables, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :is_a?, :instance_variable_get, :method, :public_method, :singleton_method, :instance_of?, :extend, :define_singleton_method, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :display, :send, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] irb(main):018:0> things.methods.length => 159 irb(main):019:0> things.each { puts "I'm ignoring the key value pair" } I'm ignoring the key value pair I'm ignoring the key value pair I'm ignoring the key value pair => {"this"=>17, "that"=>"hello", "nogots"=>nil} irb(main):020:0> nums.length => 9 irb(main):021:0> nums.length.times { |n| puts "index = " + n.to_s } index = 0 index = 1 index = 2 index = 3 index = 4 index = 5 index = 6 index = 7 index = 8 => 9 irb(main):022:0> nums.methods => [:keep_if, :values_at, :delete_at, :delete_if, :reject!, :transpose, :fill, :assoc, :rassoc, :to_h, :uniq, :uniq!, :compact, :include?, :flatten, :shuffle!, :shuffle, :flatten!, :compact!, :permutation, :combination, :repeated_combination, :product, :repeated_permutation, :bsearch, :bsearch_index, :sample, :rotate!, :&, :*, :+, :-, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :any?, :pack, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :insert, :|, :index, :rindex, :replace, :clear, :<=>, :<<, :==, :[], :[]=, :empty?, :eql?, :reverse!, :reverse, :concat, :inspect, :delete, :length, :size, :each, :slice, :slice!, :to_ary, :to_a, :to_s, :dig, :hash, :at, :fetch, :frozen?, :last, :push, :pop, :shift, :unshift, :join, :rotate, :each_index, :sort!, :sort_by!, :collect!, :map!, :select!, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :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, :slice_after, :slice_when, :chunk_while, :lazy, :tap, :public_send, :instance_variables, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :is_a?, :instance_variable_get, :method, :public_method, :singleton_method, :instance_of?, :extend, :define_singleton_method, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :display, :send, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] irb(main):023:0> nums.methods.length => 176 irb(main):025:0> def twice irb(main):026:1> yield 17 irb(main):027:1> yield 42 irb(main):028:1> puts "done" irb(main):029:1> end => :twice irb(main):030:0> twice LocalJumpError: no block given (yield) from (irb):26:in `twice' from (irb):30 from /usr/bin/irb:11:in `
' irb(main):031:0> twice { puts "i'm a block" } i'm a block i'm a block done => nil irb(main):032:0> twice { | n | puts n } 17 42 done => nil irb(main):033:0> twice { | n | puts 2*n+1 } 35 85 done => nil irb(main):034:0> def twice2 irb(main):035:1> yield 17, 42 irb(main):036:1> yield 413, 371 irb(main):037:1> end => :twice2 irb(main):038:0> twice2 { |x, y| puts " x = " + x.to_s + " y = " + y.to_s } x = 17 y = 42 x = 413 y = 371 => nil irb(main):039:0> exit bash-3.2$ exit exit Script done on Mon May 8 11:40:20 2023