Created
November 9, 2011 16:38
-
-
Save sriki77/1352007 to your computer and use it in GitHub Desktop.
Ruby Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| File.open("Ruby_Day_1.rb","r"){|file| file.each_line{|line| p line if line[/num_/]}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Print array 4 elements each time | |
| [*(1..16)].each { |v| v%4==0? p(v) : print("#{v},")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [*(1..16)].each_slice(4) {|v| p v} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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