Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Print the content of an array of sicteen numbers, four numbers at a time, using just each
- array = []
- (1..16).to_a.each { |item|
- array << item
- if (array.length == 4)
- p ar
- ar = []
- end
- }
- # Same with each_slice in Enumerable
- (1..16).to_a.each_slice(4) { |item| p item }
- # 2. Change the UI of the Tree class to accept a hash for configuration
- #
- # Download Tree code here: http://pragprog.com/titles/btlang/source_code
- # Change to initialize method of the original code
- def initialize(hash)
- @children = []
- hash.each_pair { |name, children|
- @node_name = name
- if !children.empty?
- children.each_pair { |child_name, children_nodes|
- new_hash = {child_name => children_nodes}
- @children << Tree.new(new_hash)
- }
- end
- }
- end
- # ...and call of the new UI :)
- ruby_tree = Tree.new({'grandpa' => { 'dad' => {'child 1' => {}, 'child 2' => {} }, 'uncle' => {'child 3' => {}, 'child 4' => {}}}})
- # 3. Grep the lines of a file matching a simple regular expression (/tree/) with line number
- File.open("tree.rb").each_with_index { |line, index|
- if line =~ /tree/
- p "##{index+1} #{line}"
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement