- Did encode.rb and duplcate_encoder
- .fetch in ruby
a = [ 11, 22, 33, 44 ]
a.fetch(1) #=> 22
a.fetch(-1) #=> 44
a.fetch(4, 'cat') #=> "cat"
a.fetch(4) { |i| i*i } #=> 16
- .map is the same as .collect
- ['a', 'b'].map{|x| '1'} basically replaces the a and b with 1, still in an array
- How to combine zip and map
https://stackoverflow.com/questions/5609681/ruby-adding-subtracting-elements-from-one-array-with-another-array
You could use zip:
a = [1,2,3,4]
b = [2,3,4,5]
a.zip(b).map { |x, y| y - x }
# => [1, 1, 1, 1]
There is also a Matrix class:
require "matrix"
a = Matrix[[1, 2, 3, 4]]
b = Matrix[[2, 3, 4, 5]]
c = b - a
# => Matrix[[1, 1, 1, 1]]
- .codepoints/ .ord vs bytes
https://stackoverflow.com/questions/40849265/bytes-vs-codepoints-in-ruby
bytes returns individual bytes, regardless of char size, whereas codepoints returns unicode codepoints.
s = '日本語'
s.bytes # => [230, 151, 165, 230, 156, 172, 232, 170, 158]
s.codepoints # => [26085, 26412, 35486]
s.chars # => ["日", "本", "語"]
- &:to_i is basically { |m| m.to_i}
The & is a to_Proc
https://stackoverflow.com/questions/9429819/what-is-the-functionality-of-operator-in-ruby
# The same as people.collect { |p| p.name }
people.collect(&:name)
# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)
- .cycle
a = ["a", "b", "c"]
a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
- Symbols for quotes and arrays
https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/
- Didn't realise that index can also be used on a word, not just arrays.
For e.g "key"[1] is "e".
- .gsub is an ennumerable that needs a block.
'James'.gsub(/./) {|x| "s"}
=> "sssss"
This is another way of changing each word without having to split into an array, map it, a join it back.
- .fetch in ruby
a = [ 11, 22, 33, 44 ]
a.fetch(1) #=> 22
a.fetch(-1) #=> 44
a.fetch(4, 'cat') #=> "cat"
a.fetch(4) { |i| i*i } #=> 16
- .map is the same as .collect
- ['a', 'b'].map{|x| '1'} basically replaces the a and b with 1, still in an array
- How to combine zip and map
https://stackoverflow.com/questions/5609681/ruby-adding-subtracting-elements-from-one-array-with-another-array
You could use zip:
a = [1,2,3,4]
b = [2,3,4,5]
a.zip(b).map { |x, y| y - x }
# => [1, 1, 1, 1]
There is also a Matrix class:
require "matrix"
a = Matrix[[1, 2, 3, 4]]
b = Matrix[[2, 3, 4, 5]]
c = b - a
# => Matrix[[1, 1, 1, 1]]
- .codepoints/ .ord vs bytes
https://stackoverflow.com/questions/40849265/bytes-vs-codepoints-in-ruby
bytes returns individual bytes, regardless of char size, whereas codepoints returns unicode codepoints.
s = '日本語'
s.bytes # => [230, 151, 165, 230, 156, 172, 232, 170, 158]
s.codepoints # => [26085, 26412, 35486]
s.chars # => ["日", "本", "語"]
- &:to_i is basically { |m| m.to_i}
The & is a to_Proc
https://stackoverflow.com/questions/9429819/what-is-the-functionality-of-operator-in-ruby
# The same as people.collect { |p| p.name }
people.collect(&:name)
# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)
- .cycle
a = ["a", "b", "c"]
a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
- Symbols for quotes and arrays
https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/
- Didn't realise that index can also be used on a word, not just arrays.
For e.g "key"[1] is "e".
- .gsub is an ennumerable that needs a block.
'James'.gsub(/./) {|x| "s"}
=> "sssss"
This is another way of changing each word without having to split into an array, map it, a join it back.
Comments
Post a Comment