view 1-Ruby/day2.rb @ 38:e8407d4e72dd

Add code on reflection - basically involves the object returned by "proto" message
author IBBoard <dev@ibboard.co.uk>
date Wed, 13 Sep 2017 20:11:52 +0100
parents 25c15f27b33d
children
line wrap: on
line source

#! /usr/bin/env ruby

## File handling with a block
name = 'day2.txt'

def print_file(name)
	File.open(name, 'r') do |file|
		puts name
		while line = file.gets
			puts "  #{line}"
		end
	end
end

File.open(name, 'w') do |file|
	file.puts "File contents go here"
end
print_file(name)
File.open(name, 'r+') do |file|
	# If we don't seek then we override
	file.seek(0, IO::SEEK_END)
	file.puts 'Extra content here'
end
print_file(name)
File.delete(name)

puts "File still exists!" if File.exists?(name)

# Old-skool Java style without passing a code block
my_file = File.open(name, 'w')
my_file.puts 'Non-block file content'
my_file.close() #Note: Close the file manually!
print_file(name)
File.delete(name)

## Converting a hash into an array
my_hash = { 'a' => 'one', 'b' => 'two', 'c' => 'three' }
puts my_hash.inspect
# "{}.methods" includes "to_a" method - appears to convert to an array.
puts my_hash.to_a.inspect
# However, it includes keys *and* values (as nested arrays, according to http://apidock.com/ruby/Hash/to_a).
# There's also ":keys" and ":values"
puts my_hash.keys.inspect
puts my_hash.values.inspect
# Alternatively, :collect runs over all key-value pairs as an array, so we can do:
puts my_hash.collect { |val| val[1] }.inspect

## Converting an array to a hash
# Arrays have a :to_h method, but that assumes nested arrays of key-value pairs,
# e.g. [[key1, value1], [key2, value2]]
# Looping to create a nested array structure then :to_h would work:
my_array = ['one', 'two', 'three']
temp = []
# This would be neater with :map or :collect but then we don't get the index.
# The only sensible option is an :each_with_index, but that negates the need
# for :to_h.
my_hash = {}
my_array.each_with_index do |val, idx|
	my_hash[idx] = val
end
puts my_hash.inspect

## Iterating a hash
puts "Each with single parameter gets pairs"
my_hash.each { |vals| puts "++#{vals[0]} = #{vals[1]}" }
puts "Each with two parameters gets key and value"
my_hash.each { |key, val| puts "++#{key} = #{val}" }
puts "Or you can get just keys and just values"
my_hash.each_key { |key| puts "++#{key}" }
my_hash.each_value { |value| puts "++#{value}" }

## Arrays as stacks:
my_arr = []
my_arr.push(1)
my_arr.push(2)
my_arr.push(3)
my_arr.push(4)
puts my_arr.pop()
puts my_arr.pop()
puts my_arr.pop()
puts my_arr.pop()
## Arrays as queues
my_arr.push(7)
my_arr.push(8)
my_arr.push(9)
my_arr.push(10)
puts my_arr.shift()
puts my_arr.shift()
puts my_arr.shift()
puts my_arr.shift()

## Iterating in fours

my_arr = Array(1..16)
# We need a temp place holder - we can't do ugly things with indexes
temp = []
my_arr.each do |val|
	temp.push(val)
	if temp.size == 4
		puts "#{temp[0]} #{temp[1]} #{temp[2]} #{temp[3]}"
		temp = []
	end
end

## And with each_slice
my_arr.each_slice(4) do |val|
	puts "#{val[0]} #{val[1]} #{val[2]} #{val[3]}"
end