Friday 15 January 2016

Another RUBY lesson

adam@gaia:~$ ruby hello.rb
hello.rb:2:in `<main>': undefined method `wait' for Thread:Class (NoMethodError)
adam@gaia:~$ ruby hello.rb
In another thread
adam@gaia:~$ ruby hello.rb
In thread 0In thread 5In thread 1In thread 9
In thread 4In thread 2In thread 8In thread 3
In thread 7In thread 6

adam@gaia:~$ ruby hello.rb
In thread 0
In thread 3
In thread 4
In thread 6
In thread 1
In thread 2
In thread 9
In thread 5
In thread 7
In thread 8
adam@gaia:~$ irb -r ./hello.rb
irb(main):001:0> pt_1
NameError: undefined local variable or method `pt_1' for main:Object
    from (irb):1
    from /usr/bin/irb:12:in `<main>'
irb(main):002:0> load('hello.rb')
=> true
irb(main):003:0> pt_1
NameError: undefined local variable or method `pt_1' for main:Object
    from (irb):3
    from /usr/bin/irb:12:in `<main>'
irb(main):004:0> class Point
irb(main):005:1>   attr_accessor :x, :y
irb(main):006:1> end
=> nil
irb(main):007:0>
irb(main):008:0* class Line
irb(main):009:1>   attr_accessor :pt_1, :pt_2
irb(main):010:1> end
=> nil
irb(main):011:0>
irb(main):012:0* pt_1 = Point.new
=> #<Point:0x000000014ea100>
irb(main):013:0> pt_1.x = 10
=> 10
irb(main):014:0> pt_1.y = 20
=> 20
irb(main):015:0>
irb(main):016:0* pt_2 = Point.new
=> #<Point:0x000000014a38e0>
irb(main):017:0> pt_2 = 50
=> 50
irb(main):018:0> pt_2 = 100
=> 100
irb(main):019:0>
irb(main):020:0* line = Line.new
=> #<Line:0x00000001496550>
irb(main):021:0> line.pt_1 = pt_1
=> #<Point:0x000000014ea100 @x=10, @y=20>
irb(main):022:0> line.pt_2 = pt_2
=> 100
irb(main):023:0> pt_1
=> #<Point:0x000000014ea100 @x=10, @y=20>
irb(main):024:0> line
=> #<Line:0x00000001496550 @pt_1=#<Point:0x000000014ea100 @x=10, @y=20>, @pt_2=100>
irb(main):025:0> line.pt_1
=> #<Point:0x000000014ea100 @x=10, @y=20>
irb(main):026:0> line.pt_1.x
=> 10
irb(main):027:0> line.pt_1.y
=> 20
irb(main):028:0> line.pt_2.c
NoMethodError: undefined method `c' for 100:Fixnum
    from (irb):28
    from /usr/bin/irb:12:in `<main>'
irb(main):029:0> line.pt_2.x
NoMethodError: undefined method `x' for 100:Fixnum
    from (irb):29
    from /usr/bin/irb:12:in `<main>'
irb(main):030:0> line.pt_1.y
=> 20
irb(main):031:0> class Point
irb(main):032:1>   attr_accessor :x
irb(main):033:1>
irb(main):034:1*   def y
irb(main):035:2>     x + 10
irb(main):036:2>   end
irb(main):037:1> end
=> nil
irb(main):038:0>
irb(main):039:0* class Line
irb(main):040:1>   attr_accessor :pt_1, :pt_2
irb(main):041:1> end
=> nil
irb(main):042:0>
irb(main):043:0* pt_1 = Point.new
=> #<Point:0x0000000123e000>
irb(main):044:0> pt_1.x = 10
=> 10
irb(main):045:0> pt_1.y = 20
=> 20
irb(main):046:0>
irb(main):047:0* pt_2 = Point.new
=> #<Point:0x0000000122cee0>
irb(main):048:0> pt_2 = 50
=> 50
irb(main):049:0> pt_2 = 100
=> 100
irb(main):050:0>
irb(main):051:0* line = Line.new
=> #<Line:0x000000017189e0>
irb(main):052:0> line.pt_1 = pt_1
=> #<Point:0x0000000123e000 @x=10, @y=20>
irb(main):053:0> line.pt_2 = pt_2
=> 100
irb(main):054:0> line.pt_1.y
=> 20
irb(main):055:0>
adam@gaia:~$ irb -r ./hello.rb
irb(main):001:0> Point.new.x
ArgumentError: wrong number of arguments (0 for 2)
    from /home/adam/hello.rb:2:in `initialize'
    from (irb):1:in `new'
    from (irb):1
    from /usr/bin/irb:12:in `<main>'
irb(main):002:0> Point.new(10, 20).x
=> 10
irb(main):003:0> Point.new(10, 20).y
=> 20
irb(main):004:0> Point.new(10, 20).adam
Called adam but I only respond to [:method_missing, :x, :x=, :y, :y=]
=> nil
irb(main):005:0> load('hello.rb')
=> true
irb(main):006:0> Point.new(10, 20).x
=> 10
irb(main):007:0> Point.new(10, 20).y
=> 20
irb(main):008:0> Point.new(10, 20).adam
Called adam but I only respond to [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :class, :clone, :define_singleton_method, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :initialize_clone, :initialize_dup, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :kind_of?, :method, :method_missing, :methods, :nil?, :object_id, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :respond_to?, :respond_to_missing?, :send, :singleton_class, :singleton_methods, :taint, :tainted?, :tap, :to_enum, :to_s, :trust, :untaint, :untrust, :untrusted?, :x, :x=, :y, :y=]
=> nil
irb(main):009:0> str = "Adam 6\nBrian 10\nCarol 13\nBrian 14\nAdam 23\nCarol 14"
=> "Adam 6\nBrian 10\nCarol 13\nBrian 14\nAdam 23\nCarol 14"
irb(main):010:0> puts str
Adam 6
Brian 10
Carol 13
Brian 14
Adam 23
Carol 14
=> nil
irb(main):011:0> str.lines
=> #<Enumerator: "Adam 6\nBrian 10\nCarol 13\nBrian 14\nAdam 23\nCarol 14":lines>
irb(main):012:0> str.lines.to_a
=> ["Adam 6\n", "Brian 10\n", "Carol 13\n", "Brian 14\n", "Adam 23\n", "Carol 14"]
irb(main):013:0> str.lines.map { |line| line.split }
=> [["Adam", "6"], ["Brian", "10"], ["Carol", "13"], ["Brian", "14"], ["Adam", "23"], ["Carol", "14"]]
irb(main):014:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }
=> [["Adam", 6], ["Brian", 10], ["Carol", 13], ["Brian", 14], ["Adam", 23], ["Carol", 14]]
irb(main):015:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| name }
=> {"Adam"=>[["Adam", 6], ["Adam", 23]], "Brian"=>[["Brian", 10], ["Brian", 14]], "Carol"=>[["Carol", 13], ["Carol", 14]]}
irb(main):016:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| score }
=> {6=>[["Adam", 6]], 10=>[["Brian", 10]], 13=>[["Carol", 13]], 14=>[["Brian", 14], ["Carol", 14]], 23=>[["Adam", 23]]}
irb(main):017:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| name[0] }
=> {"A"=>[["Adam", 6], ["Adam", 23]], "B"=>[["Brian", 10], ["Brian", 14]], "C"=>[["Carol", 13], ["Carol", 14]]}
irb(main):018:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| name }
=> {"Adam"=>[["Adam", 6], ["Adam", 23]], "Brian"=>[["Brian", 10], ["Brian", 14]], "Carol"=>[["Carol", 13], ["Carol", 14]]}
irb(main):019:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| name }.map { |name, name_and_scores| name_and_scores }
=> [[["Adam", 6], ["Adam", 23]], [["Brian", 10], ["Brian", 14]], [["Carol", 13], ["Carol", 14]]]
irb(main):020:0> str.lines.map { |line| line.split }.map { |name, score| [name, score.to_i] }.group_by { |name, score| name }.map { |name, name_and_scores| name_and_scores.map(&:last) }
=> [[6, 23], [10, 14], [13, 14]]
irb(main):021:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| name_and_scores.map(&:last) }
=> [[6, 23], [10, 14], [13, 14]]
irb(main):022:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| name_and_scores.map(&:last).reduce(:+) }
=> [29, 24, 27]
irb(main):023:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name_and_scores.map(&:last).reduce(:+)] }
=> [[29], [24], [27]]
irb(main):024:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }
=> [["Adam", 29], ["Brian", 24], ["Carol", 27]]
irb(main):025:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.to_h
NoMethodError: undefined method `to_h' for [["Adam", 29], ["Brian", 24], ["Carol", 27]]:Array
    from (irb):25
    from /usr/bin/irb:12:in `<main>'
irb(main):026:0> str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.to_hash
NoMethodError: undefined method `to_hash' for [["Adam", 29], ["Brian", 24], ["Carol", 27]]:Array
    from (irb):26
    from /usr/bin/irb:12:in `<main>'
irb(main):027:0> RUBY_VERSION
=> "1.9.3"
irb(main):028:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }]
=> {"Adam"=>29, "Brian"=>24, "Carol"=>27}
irb(main):029:0> str
=> "Adam 6\nBrian 10\nCarol 13\nBrian 14\nAdam 23\nCarol 14"
irb(main):030:0> str
=> "Adam 6\nBrian 10\nCarol 13\nBrian 14\nAdam 23\nCarol 14"
irb(main):031:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }]
=> {"Adam"=>29, "Brian"=>24, "Carol"=>27}
irb(main):032:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }].max_by(&:last)
\=> ["Adam", 29]
irb(main):033:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.sort_by(&:last)]
=> {"Brian"=>24, "Carol"=>27, "Adam"=>29}
irb(main):034:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.sort_by(&:last).reverse]
=> {"Adam"=>29, "Carol"=>27, "Brian"=>24}
irb(main):035:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.sort_by(&:last).reverse.lazy]
NoMethodError: undefined method `lazy' for [["Adam", 29], ["Carol", 27], ["Brian", 24]]:Array
    from (irb):35
    from /usr/bin/irb:12:in `<main>'
irb(main):036:0> Hash[str.lines.map(&:split).map { |name, score| [name, score.to_i] }.group_by(&:first).map { |name, name_and_scores| [name, name_and_scores.map(&:last).reduce(:+)] }.sort_by(&:last).reverse]
=> {"Adam"=>29, "Carol"=>27, "Brian"=>24}
irb(main):037:0>

No comments:

Post a Comment