Skip to main content

Posts

.any

@-Doing travel kata - any? vs empty? https://stackoverflow.com/questions/6245929/check-for-array-not-empty-any > [nil, 1].any? => true >> [nil, nil].any? => false - Using not in block def dont_give_me_five(start,end_)     (start..end_).count { | i | not i.to_s.include? '5' } end - Global $, Class @@ and Instance @ Variables. Never use global because it makes isolating bugs hard. http://www.rubyist.net/~slagell/ruby/globalvars.html https://stackoverflow.com/questions/12358084/whats-difference-between-and-in-a-module module A   @@a = 5 end class B   include A   puts @@a # => 5 end module A   @a = 5 end class B   include A   puts @a # => nil end -Regex cheatsheet http://www.rexegg.com/regex-quickstart.html -Array lessons c = [1,2,3,4]  => [1, 2, 3, 4] 2.4.1 :038 > c[0..3]  => [1, 2, 3, 4] 2.4.1 :039 > c[0...3] => [1, 2, 3] 2.4.1 :051 > c[0..-2] ...
Recent posts

Python

- Python 2 https://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/tree/master/ 3 / 2 = 1 3 / 2.0 = 1.5 float(3) / 2 = 1.5 Another method is using from __future__ import division >>> 3/2 1.5 Python 3 doesn't need to use import - Strings Python 3 from __future__ import print_function print('Hello World') vs Python 2 print 'Hello world' len('  ') checks string length  s = 'Hello World' # Grab everything past the first term all the way to the length of s which is len(s) s[1:] => "ello World' # Grab everything UP TO the 3rd index s[:3]=> 'Hel' #Everything s[:] => 'Hello World' # Grab everything, but go in step sizes of 2 s[::2] => 'HloWrd' # We can use this to print a string backwards s[::-1] =>  'dlroW olleH' - Python 2 More Strings s = 'STRING' 1. print 'Place another string with a mod and s: %s' %(s) Place anothe...

Ruby Formatting, ljust rjust, regex, .count

- Didn't realise that nil.to_i = 0 https://stackoverflow.com/questions/11029256/difference-between-nil-blank-and-empty - Interesting that .count and .size doesn't just count number of elements in array. If I did (1..5).count or .size # => 5 If I did .length, it would have an error. .count { | i | not i.to_s.include? '5' } can be used as a block. - .reject It's cool that reject blocks can be used in this manner: 1) .reject { |e| e.to_s.include?('5') } 2) .reject { |n| n.to_s =~ /5/ } Both achieve the same thing. - Ruby formatting. This is very new to me. ("%-5s" % "teg") => "teg  " It's like saying the format is 5 characters in a sring. https://idiosyncratic-ruby.com/49-what-the-format.html - .ljust expands the string or if there is an integer, fills the strings to the number of characters. "hello".ljust(4)            #=> "hello" "hello".ljust(20)           #=>...

Ternary Operators, Hash and Regex

- Doing name_shuffler.rb kata and competitive_eating scoreboard.rb - More on gsub Basically, anything in [xxx] will be replaced.  "hello".gsub(/[aeiou]/, '*')                  #=> "h*ll*" the \1 means the group number. Has to come with a ([xxx]). Can be \any_number "hello".gsub(/([aeiou])/, '<\1>')             #=> "h<e>ll<o>" Using a block, sub everything  "hello".gsub(/./) {|s| s.ord.to_s + ' '}      #=> "104 101 108 108 111 " This is the use of a group name. It says to replace everything with the group name with similar words from the group. ?<groupname>[xxx] and \k<groupname>. "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}" This is to replace specifically.  'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*" - T...

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

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