Skip to main content

Posts

Showing posts with the label course 101

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

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