Created
October 29, 2011 12:24
-
-
Save jdoig/1324382 to your computer and use it in GitHub Desktop.
7 Languages in 7 Weeks: 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
(1..16).to_a.each {|i| i % 4==0 ? puts(i) : print(i) } |
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).to_a.each_slice(4) {|i| p i} |
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(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 } |
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
def my_grep(file, phrase) | |
puts File.open(file).each_line.grep /.*#{phrase}.*/ | |
end | |
my_grep('C:\Test\test.txt', 'ruby') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice - especially the grep solution - I had no idea that was possible.