Skip to main content

Posts

Showing posts from July, 2017

take

- learnt that .take is different to that of .first. take returns it as an array while first returns as a sring if its the first object. a = [1, 2, 3] a.first 3 == a.take 3 a.first != a.take 1 #=> '1' != ['1'] - In Regex, ^ opens and $ closes if you want first character or last character regx ^a|a$ mean it has to start with a or end with a Another and operator in regex for AND https://stackoverflow.com/questions/6437516/how-to-use-and-in-ruby-regex https://stackoverflow.com/questions/3041320/regex-and-operator - Regex or operator ^ten.*|.*?end$ starts with ten or ends with end - Learnt %r which is similar to having a / ... / https://stackoverflow.com/questions/12384704/the-ruby-r-expression - More on regex https://stackoverflow.com/questions/8020848/and-or-operator-in-regular-expression http://www.regular-expressions.info/ http://www.rexegg.com/regex-quickstart.html - Ruby resource http://ruby-for-beginners.rubymonstas.org/index.html - ...

.chars, &:, delete_at, swap, scan

- Did duplicate_count.rb - Learnt .chars actually splits a string into an array of separate characters. But this is case sensitive. - To transform an array of uppercase and lowercase letters to lowercase .select{|item| item.respond_to? :downcase}.map(&:downcase) : before downcase is to see if downcase method exist. - respond_to? is to see if a method exists within a class. https://stackoverflow.com/questions/17893977/confused-about-respond-to-method https://stackoverflow.com/questions/6849722/confused-about-respond-to-vs-respond-to - .downcase works on a string but not array. - .map(&:downcase) is the same as .map { |keyword| keyword.downcase } The &: represents a block. - .group_by groups the hashes in further array depending on condition (1..6).group_by {|i| i%3}   #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]} b = ['a','a','b','b'] b.group_by(&:itself) => {"a"=>["a", "a"], ...

More Arrays

- Learnt from delete_nth kata - .combination gives the unique permutation. for example a = [1, 2, 3, 4] a.combination(1).to_a  #=> [[1],[2],[3],[4]] a.combination(2).to_a  #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] a.combination(3).to_a  #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]] a.combination(4).to_a  #=> [[1,2,3,4]] -.product gives all permutations a = [4,5,6] b= [1,2,3] a.product(b) => [[4, 1], [4, 2], [4, 3], [5, 1], [5, 2], [5, 3], [6, 1], [6, 2], [6, 3]] - .index tells you the index of a object a = [1,1,2,3] a.index(1) => 0 a.index(2) => 2 - Ruby Cheatsheet https://www.shortcutfoo.com/app/dojos/ruby-arrays/cheatsheet - Learnt delete_at() and .rindex(n) rindex basically is the reverse of .index which gets the last index of the value in the array. - .delete_if and .reject are quite similar - a = Hash.new(0) => {} a["good"] = 1 => 1 a => {good => 1} a["good"] += 1 => 2 a => {good => ...

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

Reject, Select, is_a?, count, %w, regex

- Learnt EC2 basics from AWS and security groups - Learnt .floor and .ceil puts 2.67.ceil == 2 #=> false, because 2.67.ceil is 3 puts 2.67.floor == 2 #=> true, because true - Learnt is_a? Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj. module M;    end class A   include M end class B < A; end class C < B; end b is an object of Class B and is A is the superclass of B. b will return true if B it belongs to class B or class A but not class C even if it is a subset of class B. It will return true if M is a module of class A or class B. b = B.new b.is_a? A          #=> true b.is_a? B          #=> true b.is_a? C          #=> false b.is_a? M          #=> true b.kind_of? A       #=> true b.kind_of? B       #=> true b.kind_of? C ...

4th July Independence Day: Learning the sq rt

- Learnt from kata how to use Random.new & rand(1..6) https://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby - Learnt that = is used for assignment and == for comparison in ruby. - learnt what Math.sqrt(x) is. - return true/false stops the program and will not print any put statements after - The ternary operator works in a way that is like puts 1 ? 2 : 3 There has to be a method in front - This is an interesting algorithm for looking at determining the sqrt using maths. Quite amazing but impractical for more than 10 million. https://www.codecademy.com/en/forum_questions/511126448c0a9ee18600695f - Learnt that modulus % is very interesting. x % 1 == 0 but if x is a float, then it would be false instead of true.

The Beginning

Today I covered the remainder of S3 and the introduction of EC2. I read the first pages of ruby under the microscope. I learned what tokenization is and parsing. There is quite some complexity to tokenization and parsing. Ruby is built on top of C. I am picking up the basics From reading the ruby docs, I learnt: - Numbers, strings, Arrays, Hashes are actually called Literals - if, unless, while, until, for, break, next, redo are called Control Expressions.  - Ternary if can be written as ? : input_type = gets =~ /hello/i ? "greeting" : "other" is the equivalent of: input_type =   if gets =~ /hello/i     "greeting"   else     "other"   end - "unless" is the equivalent of "not true - You can put the else condition but the elsie after unless. - case uses the === method. 1)  case "2" when /^1/, "2"   puts "the string starts with one or is '2'" ...