Skip to content

Instantly share code, notes, and snippets.

@japaz
Created October 3, 2011 07:31
Show Gist options
  • Save japaz/1258624 to your computer and use it in GitHub Desktop.
Save japaz/1258624 to your computer and use it in GitHub Desktop.
7L7W Ruby - Day 2
=begin
Print the contents of an array of sixteen numbers, four numbers
at a time, using just each.
=end
i=0
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].each do |x|
i=i+1
print "#{x} "
puts "" if i%4==0
end
#Now, do the same with each_slice in Enumerable.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].each_slice(4) {|a| p a}
=begin
The Tree class was interesting, but it did not allow you to specify
a new tree with a clean user interface. Let the initializer accept a
nested structure with hashes and arrays. You should be able to
specify a tree like this: {’grandpa’ => {’ dad’ => {’child 1’ => {}, ’child
2’ => {} }, ’uncle’ => {’child 3’ => {}, ’child 4’ => {} } } }.
=end
class Tree
attr_accessor :children, :node_name
def initialize(name, children=[])
@children = children
@node_name = name
end
def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block}
end
def visit(&block)
block.call self
end
end
ruby_tree = Tree.new ("grandpa",
[Tree.new("dad",[Tree.new("child 1"), Tree.new("child 2")]),
Tree.new("uncle",[Tree.new("child 3"), Tree.new("child 4")])])
puts "Visiting a node"
ruby_tree.visit {|node| puts node.node_name}
puts
puts "visiting entire tree"
ruby_tree.visit_all {|node| puts node.node_name}
puts
puts
puts
class Tree2
attr_accessor :children, :node_name
def initialize(nested)
@children = nested.values[0].map {|key, value| Tree2.new(Hash[key, value])}
@node_name = nested.keys[0]
end
def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block}
end
def visit(&block)
block.call self
end
end
ruby_tree2 = Tree2.new({'grandpa' => {'dad' => {'child 1' => {}, 'child 2' => {} }, 'uncle' => {'child 3' => {}, 'child 4' => {} } } })
puts "Visiting a node"
ruby_tree2.visit {|node| puts node.node_name}
puts
puts "visiting entire tree"
ruby_tree2.visit_all {|node| puts node.node_name}
puts
=begin
Write a simple grep that will print the lines of a file having any
occurrences of a phrase anywhere in that line. You will need to do
a simple regular expression match and read lines from a file. (This
is surprisingly simple in Ruby.) If you want, include line numbers.
=end
File.open("sampleFile.txt", "r") {|aFile| aFile.each_line {|line| puts "#{aFile.lineno}: #{line}" unless line.match('f.la') == nil}}
# Find out how to access files with and without code blocks.
# With out block
aFile = File.new("testfile", "r")
# ... process the file
aFile.close
# With blocks
File.open("testfile", "r") do |aFile|
# ... process the file
end
# What is the benefit of the code block?
# auto close of the file.
# How would you translate a hash to an array?
a = {1=> "one", 2 => [2,"two"], 3 => "three"}
a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
a.to_a # => [[1, "one"], [2, [2, "two"]], [3, "three"]]
# Can you translate arrays to hashes?
hash = [[:first_name, 'Shane'], [:last_name, 'Harvie']].inject({}) do |result, element|
result[element.first] = element.last
result
end # => {:first_name=>"Shane", :last_name=>"Harvie"}
hash.class # => Hash
# Next method also works, but I don't know why
a = Hash[*[[:first_name, 'Shane'], [:last_name, 'Harvie']].flatten] # => {:first_name=>"Shane", :last_name=>"Harvie"}
a.class # => Hash
# For flatted arrays
a= [1, "one", 2, "two"] # => [1, "one", 2, "two"]
Hash[*a] # => {1=>"one", 2=>"two"}
# Can you iterate through a hash?
h = { "a" => 100, "b" => 200 } # => {"a"=>100, "b"=>200}
h.each {|key, value| puts "#{key} is #{value}" }
#a is 100
#b is 200
#=> {"a"=>100, "b"=>200}
# You can use Ruby arrays as stacks. What other common data structures do arrays support?
# you can treat arrays as stacks, sets, queues, dequeues, and fifos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment