Skip to main content

Posts

Showing posts from September, 2016

Why am I going through Launch School?

Going through Launch School (LS) is tough. I need a compelling reason to keep moving forward. If I finished LS, I am going to obtain substantial knowledge of how to create web application. Creating web application is essential to launching a new startup. I want to create a web application that creates value for society. Creating web application is my chosen way to start my entrepreneurship journey because it is cheap, growing fast and its impact can be very big. I want to impact people's life in a big way. I want to create huge value for people. Knowledge about web application can be used in working for a startup. In case joining other people's startup is a better choice than doing your own startup, then this knowledge can be valuable. You could work for a comfortable wage or even acquire some equity working for a startup. The problem solving skills I could learn at LS is similar to that of an engineer. The temperament, techniques and skills in tackling problem I can prac

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

Problem Solving - Refactored

I am going to outline how I approach problem solving. The relative importance and the amount of effort/time required for each is stated as a percentage beside each topic. I borrowed some idea from George Polya's How to Solve It Thoroughly Understand the Problem (30%) When encountering hard problem , you need to deeply understand the problem at hand. Take a paper and list down all known facts and data and what the question is trying to find. Sketch out the problem if applicable. Visualize the problem in your head. A lot of times, we only have to understand the problem well, then the solution will obvious. Have a Plan (20%) You need to have an outline of how you are going to tackle the problem. You need to have a logical pathway that will ultimate produce outcome (nothing to do with coding syntax yet). Without a plan, you are just randomly poking around and got lucky. No hard problem ever gets solved without a plan. Plan using pseudo-code, pen & paper or flowchart. Use wh

Rules to write method

Rules  1. It should not both return a value or perform some side-effect. It should only do one. Eg: Your method should not do this def do_multiple(array)      array << "another_element" end The method return a modified array , at the same time, mutates the original array. 2. It should very limited things at a time. It should do one thing at a time. 3. If it mutates a value, use "!" 4. If it print something , use keywords like "say_xxx" or "print_xxx" 5. A good program is built with many small methods.

yaml file

1. type require 'yaml' at the top of your code. 2. Next  type    MESSAGES = YAML . load_file ( 'calculator.yml' ) 3. For example your YAML file is like this, welcome : "Welcome to Calculator! Enter your name:" valid_name : "Make sure to end a valid name." Then, all your MESSAGES is a hash file like this. { "welcome" => "Welcome to Calculator! Enter your name:" , "valid_name" => "Make sure to end a valid name." } You can extract the message with code like this: MESSAGES["welcome"] 3. If you want to internationalize your program. Your yaml file will have another layer of language keys like en or es en : welcome : "Welcome to Calculator! Enter your name:" valid_name : "Make sure to enter a valid name." es : welcome : "Bienvenido a la calculadora! Entre su nombre:" valid_name : "Asegúrese de entrar un nombre válido."

Case statement

General Method 1 The case statement is used as follows: operation = 1 case operation when 1    puts 'operation is successful' when 0    puts 'operation is a failure' end The compiler will compare the value after 'when' , which is 1 with operation. If it evaluates to true, then the code after "when 1" is executed. Results : "operation is successful" Method 2 Another method of use is : operation = 1 result = case operation              when 1               1 + 1              when 2               1 - 1              end puts result Here , result will print 2 because "1+1" is executed. This returns 2 and passed into "result" variable.

local variable scope

Method- local variable Local variables inside a method is not accessible outside the method. Eg: def method   x = 2 end puts x This will result in an error . To access variables outside a method. You need to pass in parameters/arguments into the method. x = '' def method(x)   x = 2 end puts method(x) However, in this case, x is still ' ' . The puts only print the return value of the method , which is 2.  The method do not mutate the caller. Local variable - block scope. Local variable that sits outside a block is ACCESSIBLE to inside the block. While local variable that is initialize inside a block is NOT ACCESSIBLE outside the block. Eg: x = "Hello" loop do    puts x    break end This is ok Eg: loop do     x = "Hello"    break end puts x This will result in an error. PS: Please take note that IF statement is not considered a block. Therefore any variables initialized in an IF statement is accessible

Method's output vs return

General Return value is what is returned by the method. Output is what is done by the method For example Puts return a 'nil' object. However, puts's output displays the arguments into the screen. Return value With return value, you could chain methods. For example a = [1,2,3] a.map { | num | num * 2 }.join This will return an array [2,4,6]. Then , a join method is called upon the new array resulting in a string "246".