view 1-Ruby/day2-irb.output @ 19:ddcd809319ac

Add first two Day 2 exercises
author IBBoard <dev@ibboard.co.uk>
date Wed, 04 Jan 2017 21:01:13 +0000
parents
children
line wrap: on
line source

day2.rb(main):001:0> #! /usr/bin/env ruby
day2.rb(main):002:0* 
day2.rb(main):003:0* ## File handling with a block
day2.rb(main):004:0* name = 'day2.txt'
=> "day2.txt"
day2.rb(main):005:0> 
day2.rb(main):006:0* def print_file(name)
day2.rb(main):007:1> 	File.open(name, 'r') do |file|
day2.rb(main):008:2* 		puts name
day2.rb(main):009:2> 		while line = file.gets
day2.rb(main):010:3> 			puts "  #{line}"
day2.rb(main):011:3> 		end
day2.rb(main):012:2> 	end
day2.rb(main):013:1> end
=> :print_file
day2.rb(main):014:0> 
day2.rb(main):015:0* File.open(name, 'w') do |file|
day2.rb(main):016:1* 	file.puts "File contents go here"
day2.rb(main):017:1> end
=> nil
day2.rb(main):018:0> print_file(name)
day2.txt
  File contents go here
=> nil
day2.rb(main):019:0> File.open(name, 'r+') do |file|
day2.rb(main):020:1* 	# If we don't seek then we override
day2.rb(main):021:1* 	file.seek(0, IO::SEEK_END)
day2.rb(main):022:1> 	file.puts 'Extra content here'
day2.rb(main):023:1> end
=> nil
day2.rb(main):024:0> print_file(name)
day2.txt
  File contents go here
  Extra content here
=> nil
day2.rb(main):025:0> File.delete(name)
=> 1
day2.rb(main):026:0> 
day2.rb(main):027:0* puts "File still exists!" if File.exists?(name)
=> nil
day2.rb(main):028:0> 
day2.rb(main):029:0* # Old-skool Java style without passing a code block
day2.rb(main):030:0* my_file = File.open(name, 'w')
=> #<File:day2.txt>
day2.rb(main):031:0> my_file.puts 'Non-block file content'
=> nil
day2.rb(main):032:0> my_file.close() #Note: Close the file manually!
=> nil
day2.rb(main):033:0> print_file(name)
day2.txt
  Non-block file content
=> nil
day2.rb(main):034:0> File.delete(name)
=> 1
day2.rb(main):035:0> 
day2.rb(main):036:0* ## Converting a hash into an array
day2.rb(main):037:0* my_hash = { 'a' => 'one', 'b' => 'two', 'c' => 'three' }
=> {"a"=>"one", "b"=>"two", "c"=>"three"}
day2.rb(main):038:0> puts my_hash.inspect
{"a"=>"one", "b"=>"two", "c"=>"three"}
=> nil
day2.rb(main):039:0> # "{}.methods" includes "to_a" method - appears to convert to an array.
day2.rb(main):040:0* puts my_hash.to_a.inspect
[["a", "one"], ["b", "two"], ["c", "three"]]
=> nil
day2.rb(main):041:0> # However, it includes keys *and* values (as nested arrays, according to http://apidock.com/ruby/Hash/to_a).
day2.rb(main):042:0* # There's also ":keys" and ":values"
day2.rb(main):043:0* puts my_hash.keys.inspect
["a", "b", "c"]
=> nil
day2.rb(main):044:0> puts my_hash.values.inspect
["one", "two", "three"]
=> nil
day2.rb(main):045:0> # Alternatively, :collect runs over all key-value pairs as an array, so we can do:
day2.rb(main):046:0* puts my_hash.collect { |val| val[1] }.inspect
["one", "two", "three"]
=> nil
day2.rb(main):047:0> 
day2.rb(main):048:0* ## Converting an array to a hash
day2.rb(main):049:0* # Arrays have a :to_h method, but that assumes nested arrays of key-value pairs,
day2.rb(main):050:0* # e.g. [[key1, value1], [key2, value2]]
day2.rb(main):051:0* # Looping to create a nested array structure then :to_h would work:
day2.rb(main):052:0* my_array = ['one', 'two', 'three']
=> ["one", "two", "three"]
day2.rb(main):053:0> temp = []
=> []
day2.rb(main):054:0> # This would be neater with :map or :collect but then we don't get the index.
day2.rb(main):055:0* # The only sensible option is an :each_with_index, but that negates the need
day2.rb(main):056:0* # for :to_h.
day2.rb(main):057:0* my_hash = {}
=> {}
day2.rb(main):058:0> my_array.each_with_index do |val, idx|
day2.rb(main):059:1* 	my_hash[idx] = val
day2.rb(main):060:1> end
=> ["one", "two", "three"]
day2.rb(main):061:0> puts my_hash.inspect
{0=>"one", 1=>"two", 2=>"three"}
=> nil
day2.rb(main):062:0>