# Seven Languages in Seven Weeks: Ruby Day 2

Day 2 continues where day 1 left off. The exercises for this chapter were definitively more fun than the first one.

So without further ado, here they are:

Print the contents of an array of 16 numbers, four numbers at a time. First using only each, then using each_slice.

```# question 1
my_array = [23, 65, 23, 43,
11, 2, 1, 2,
9, 8, 7, 6,
56, 65, 67, 76]

# question 1 part 1
(0..3).each {|a| puts "#{my_array[a * 4]}, #{my_array[(a * 4) + 1]}, #{my_array[(a * 4) + 2]}, #{my_array[(a * 4) + 3]}"}

# question 1 part 2
my_array.each_slice(4) {|a| p a}
```

Modify the Tree class presented in the chapter to accept a nested structure in it’s initializer. We want to be able to specify a tree with a hash like this: {‘grandpa’ => {‘dad’ => {‘child 1’ => {}, ‘child 2’ => {}}, ‘uncle’ => {‘child 3’ => {}, ‘child 4’ => {}}}}

```# question 2
class Tree
attr_accessor :children, :node_name

def initialize(tree_structure)
@node_name = tree_structure.keys.first
@children = []

tree_structure[@node_name].keys.each do |key|
childrens = Hash[key, tree_structure[@node_name][key]]
@children.push(Tree.new(childrens))
end
end

def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block}
end

def visit(&block)
block.call self
end
end

ruby_tree = Tree.new({'grandpa' => {'dad' => {'child 1' => {}, 'child 2' => {}}, 'uncle' => {'child 3' => {}, 'child 4' => {}}}})
```

Write a simple grep that will print the lines and line numbers of all occurrences of an expression within a file. Use regular expressions to match the expression.

```# question 3
def grep(word)
lineNumber = 0

file = File.open("day2q3FILE.txt").each do |line|
lineNumber += 1

if /#{word}/.match(line)
puts "(#{lineNumber}) #{line}"
puts
end
end
end

grep("amet")
```

For this question I made myself a sample file. To generate some text I used a Lorem Ipsum generator on the web. Lorem Ipsum is pretty much a standard for dummy text in application mock ups.

For this exercise this text was perfect. In the field, I would have preferred using real or as close to real data as I could find. You can find a pretty good argument against using Lorem Ipsum type text on this website: Death to Lorem Ipsum. I would also add that even though dummy text may help with layout, it can also lead you to make false assumptions regarding this same layout. This is because the fake text may be of a different length or density than the actual text.

I have included the sample file for reference.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed mauris nibh.
Aenean lobortis mollis orci sed dictum. Aenean in erat libero.
Nam bibendum, turpis sit amet lacinia volutpat, ante nisl egestas arcu, sed ornare lorem sapien ut tortor.
Morbi varius mi molestie dui rhoncus sed molestie ante imperdiet.
Proin facilisis, nisl quis consectetur pharetra, ligula lacus rutrum felis, et rhoncus nulla diam a tellus.
Donec eget enim enim, sed tempus tortor. Cras vitae semper nisl.
Quisque vitae lectus et tellus malesuada convallis.

And this concludes day 2.

## 3 thoughts on “Seven Languages in Seven Weeks: Ruby Day 2”

1. Yves Dubois says:

You could use the following as a simpler way of printing the contents of the array using “each”:

(0..3).each {|a| p my_array[a * 4..a * 4 + 3]}

2. Your solution is definitively more concise. I like it!