Skip to main content

Posts

Showing posts with the label symbols for quotes and arrays

.map, .fetch, .ord, word indexes, .cycle

- 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...