Created
November 12, 2011 22:10
-
-
Save philosodad/1361196 to your computer and use it in GitHub Desktop.
7 languages Ruby Day 2
This file contains 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
array2 = [] | |
16.times{array2 << rand(100)} | |
[0,1,2,3].each{|index| 4.times{print array2[index], " ";index+=4};print "\n"} |
This file contains 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 simple_grep file, pattern | |
File.open(file, "r") do |file| | |
while x = file.gets | |
puts x if x.match(pattern) | |
end | |
end | |
end | |
simple_grep ARGV[0], ARGV[1] |
This file contains 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 opts | |
@node_name = opts.keys.first | |
@children = [] | |
opts[@node_name].each{|k,v| x = {k=>v}; @children << Tree.new(x) } | |
end | |
def visit_all &block | |
visit &block | |
@children.each{|c| c.visit_all &block} | |
end | |
def visit &block | |
block.call self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment