Skip to content

Instantly share code, notes, and snippets.

@jdoig
Created October 29, 2011 12:24
Show Gist options
  • Save jdoig/1324382 to your computer and use it in GitHub Desktop.
Save jdoig/1324382 to your computer and use it in GitHub Desktop.
7 Languages in 7 Weeks: Day 2
(1..16).to_a.each {|i| i % 4==0 ? puts(i) : print(i) }
(1..16).to_a.each_slice(4) {|i| p i}
class Tree
attr_accessor :children, :node_name
def initialize(hash)
@node_name = hash.keys.first
@children = hash[@node_name].collect{|k,v| Tree.new({k=>v})}
end
def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block}
end
def visit(&block)
block.call self
end
end
family = {'grandpa'=>{'dad'=> {'child 1'=>{}, 'child 2'=>{}},'uncle'=>{'child 3'=>{}, 'child 4' =>{}}}}
family_tree = Tree.new(family)
family_tree.visit_all {|c| puts c.node_name }
def my_grep(file, phrase)
puts File.open(file).each_line.grep /.*#{phrase}.*/
end
my_grep('C:\Test\test.txt', 'ruby')
@jomontanari
Copy link

Nice - especially the grep solution - I had no idea that was possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment