Skip to main content

Posts

Showing posts with the label Frequently Used Method

Frequently Used Method - Hash

Hash 1. include? , has_key? It will return true if the object passed in match the KEY in the hash. eg: a = {:A => 200 , :B => 300} a.include?(200)  returns false a.has_key?(:A) return true 2. each { |key, value| } It will iterate through the hash key once. Returns the original full hash. eg: a = {:A => 200 , :B => 300} a.each do  |key, value|      puts key       puts value  end # A # 200 # B # 300 # return  {:A => 200 , :B => 300} 3. fetch It will return the value based on the hash key inputted eg: a = {:A => 200 , :B => 300} a.fetch(:A) # return  200 4. key It will return the key based on the value passed in. eg: a = {:A => 200 , :B => 300} a.key(200) # return :A

Frequently Used Method - Array

Array 1. include? It will return true if the object passed in match the element in the array. eg: a = [1,2,3] a.include?(2)  returns true 2. map / map.with_index Iterates through the array , then , create a new array with values returns by the block of code. Eg: to double all integers in an array array = [1,2,3] doubled = array.map { |element| element * 2  } doubled = [2,4,6] Another short hand method of writing map is this Eg: to convert an array of strings into integers. a = ['2', '3'].map(&:to_i) a = [2,3] This will return [2, 3]. Note (shortcut "&:" method will only work for method that doesn't take an argument). It is the same as the method below. ['2', '3'].map { |element| element.to_i } 3. [] =  It will return the value at index position passed in. Eg : a = [1,2,3,4] a[0] = 1 a[-2] = 3 4. each / each_with_index Iterate through the array. Returns the original array. The variant with_index creates...

Frequently Used Method - Array

Array 1. include? It will return true if the object passed in match the element in the array. It follows this format, population.include?(sample) eg: a = [1,2,3] a.include?(2)  returns true 2. map / map.with_index  aka.  collect Iterates through the array , then , create a new array with values returns by the block of code. Eg: to double all integers in an array array = [1,2,3] doubled = array.map { |element| element * 2  } doubled = [2,4,6] Another short hand method of writing map is this Eg: to convert an array of strings into integers. a = ['2', '3'].map(&:to_i) a = [2,3] This will return [2, 3] It is the same as the method below. ['2', '3'].map { |element| element.to_i } 3. [] =  It will return the value at index position passed in. Eg : a = [1,2,3,4] a[0] = 1 a[-2] = 3 In OOP, if you want to override this method for your object. Eg: def []=(key, value) end 4. each / each_with_index Iterate through the array. ...

Frequently Used Method - String

These are the method frequently used for strings. 1. []= String can act like an array.  Eg: "hello"[1]              # return "e" "hello"[-2]             # return "l" "hello"[1..-1]          # return "ello" 2. chomp It will remove any record separator like enter key when called. Return the string with record separator removed. 3. to_i Convert strings into integer. If non-numeric character is called, then it will return zero. Eg: "1".to_i      # return 1 "s".to_i      # return 0 4. capitalize , upcase, downcase Capitalize convert first letter to capital. Upcase convert whole string to capital. Downcase convert whole string to lowercase. Return the modified string. 5. chars Split the string into individual character's array. Eg: "hello".chars    # return ['h','e','l','l','o'] 6. gsub Replace characters with another character Eg...