Skip to content

Instantly share code, notes, and snippets.

@sriki77
Created November 9, 2011 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sriki77/1352007 to your computer and use it in GitHub Desktop.
Save sriki77/1352007 to your computer and use it in GitHub Desktop.
Ruby Day 2
File.open("Ruby_Day_1.rb","r"){|file| file.each_line{|line| p line if line[/num_/]}}
Print array 4 elements each time
[*(1..16)].each { |v| v%4==0? p(v) : print("#{v},")}
[*(1..16)].each_slice(4) {|v| p v}
class Tree
attr_accessor :children, :node_name
def initialize(t={})
t.each do |k,v|
@node_name=k
@children=v.collect { |c| Tree.new(Hash[*c]) }
end
end
def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block}
end
def visit()
yield self
end
private :visit
end
ruby_tree=Tree.new({'grandpa'=>{'dad'=>{'child 1'=>{},'child 2'=>{}},'uncle'=>{'child 3'=>{},"child 4"=>{}}}})
pp "Visiting entire tree"
ruby_tree.visit_all {|node| pp node.node_name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment