Skip to main content

Summation

- Learnt different summation from http://viarails.net/q/How-to-sum-an-array-of-numbers-in-Ruby
Never knew that in RoR, you can do .sum

1)
sum = 0
[1, 2, 3].each do |i|
  sum += i
end

puts sum   # 6

2) [1, 2, 3].inject(0) {|sum, i|  sum + i }
3) [1, 2, 3].inject(0, &:+)

2) .fill is like substituting. Opposite of .fill is .drop

array = [1,2,3,4,5]
array.fill(12, array.size, 4)
# => [1, 2, 3, 4, 5, 12, 12, 12, 12]

array.fill(n, i, x)
n = the value
i = the index
x = the number of times

________________
arr = arr.drop(i)

> arr = [1,2,3,4,5]
 => [1, 2, 3, 4, 5]
> arr.drop(2)
 => [3, 4, 5]

- .zip

[1,2,3].zip([3,4,5]) is [ [1,3], [2,4], [3,5] ]


a = [ 4, 5, 6 ]
b = [ 7, 8, 9 ]
[1,2,3].zip(a, b)      #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
[1,2].zip(a,b)         #=> [[1, 4, 7], [2, 5, 8]]
a.zip([1,2],[8])       #=> [[4,1,8], [5,2,nil], [6,nil,nil]]

- What is interesting is that .zip can do lots more and is related to transpose.
https://stackoverflow.com/questions/18563824/using-enumerablezip-on-an-array-of-arrays

e = [ [1,2], [3,4] ]

name, *rows = e
name.zip(*rows) #=> [[1, 3], [2, 4]]

e.transpose == zipped  #=> true

- .each_slice is interestingly new. It can split the array into groups of n

a = [0, 1, 2, 3, 4, 5, 6, 7]
a.each_slice(3).to_a # => [[0, 1, 2], [3, 4, 5], [6, 7]]

- .slice basically extract out the values you want into a new array
a #=> [1, 2, 3]
.slice(start index, length) => array
c = a.slice(1,2) #=> [2, 3]

.slice(index). returns an obj
c = a.slice(1) #=> 2

.slice(range) => array
c = a.slice(0..2) #=> [1, 2, 3]

- Learnt a few things about
https://stackoverflow.com/questions/1801516/how-do-you-add-an-array-to-another-array-in-ruby-and-not-end-up-with-a-multi-dim

- each_with_index is different from with_index in that you can start from any index of your choice with_index(n)

For example:

[:foo, :bar, :baz].each.with_index(2) do |value, index|
    puts "#{index}: #{value}"
end

[:foo, :bar, :baz].each_with_index do |value, index|
    puts "#{index}: #{value}"
end

Outputs:

2: foo
3: bar
4: baz

0: foo
1: bar
2: baz

https://stackoverflow.com/questions/20258086/difference-between-each-with-index-and-each-with-index-in-ruby

a=[11,22,31,224,44].each_with_index { |val,index| puts "index: #{index} for #{val}" if val < 30}
  index: 0 for 11
  index: 1 for 22
  => [11, 22, 31, 224, 44]

- A good resource for arrays is here when it comes to .push, .pop, .insert, .delete_at

https://gistpages.com/posts/ruby_arrays_insert_append_length_index_remove

- Another good resource is https://stackoverflow.com/questions/1801516/how-do-you-add-an-array-to-another-array-in-ruby-and-not-end-up-with-a-multi-dim

basically a.concat(b) == a + b
== a.push(*b) #note the asterisk
== b.unshift  *a # note that b is the receiver. Unshift basically puts the input array a to the front of b
== a.insert(a.length, *b)

Without the asterisk, if a = [1,2,3], b = [2,3,4]
a.push b = [1,2,3, [2,3,4] ]
The * flatten the array b

- I also learnt that you can decide multiple objects to access in an array.
a = [1,2,3]
a[ the index, the next number of ojects after including the current inde]
for example, a[0, 2] => [1,2]
a[1,1] = 2
a[3,0] = []

You can also use range
a[0..2] = [1,2,3]
a[0..4] = [1,2,3]
 for a[0,4]

Comments