Skip to main content

Posts

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl
Recent posts

Introduction to Django framework Part 14

 Introduction to Django framework Table of content What is web framework Django  Django features  Flask vs Django  Django setup and installation Create a django project Register an app and run the project What is web framework A web framework is a collection of modules, packages and libraries to speed-up the web development. Django Django is a Python Web framework. It follows MVT Design Pattern. M stands for Model which deals with data accces layer. T stands for template engine which represents the presentation layer and View is the controller that holds the business logic of the application.   Django features:   1. Rapid development - easy to setup and build a new project. 2. Django provides different components like authentication, model, modelforms, etc, which helps in development 3. Security - Django provides features to secure the project. 4. Scalability - Django applications are highly scalable. Other Python Web Frameworks Cherry pie Web2Py Pyramid Useful VS Code Extensions Pytho

Functional Programming in Python 3 - part 12

 Functional Programming in Python 3 - part 12 Table of content Functional programming Pure function Recursion Variables in immutable First class function Built-in high order function map function filter function Lambda Expressions Functional Programming Functional programming is a style of programming where almost every operation is performed with the help of functions. Functions are considered as first class citizens. Below are some of the guidelines for functional programming in python. 1. Pure Function : Every function is pure i.e. their behavior do not change for the same input. A pure function gives a consistent output (same output every time for the same input).  The arguments passed to the function should not be modified. Below example illustrate the pure function implementation. A function takes string as argument and returns its lowercase version without modifying the passed object.   def pure_fun(input_string): new_string = input_string.lower() # do not modify the argu

Ruby on Rails Part 4 - Exception Handling

  Ruby on Rails Part 4 - Exception Handling  Table of content Exception Handling retry raise ensure else  catch and throw Exception classes Exception Handling Enclose the code that could raise an exception with a begin/end block and use rescue clauses to tell Ruby the types of exceptions that you want to handle. The syntax for exception handling : 
 begin  
      #- statements
 rescue OneTypeOfException       #-
 handle the exception rescue AnotherTypeOfException       #- 
handle the exception else       
# Other exceptions
 ensure 
      # ensure block is always executed
 end 
 Everything from begin to rescue is protected 

in the block. 
If an exception occurs during the execution of this block of code, control is passed to the 

block between rescue and end.
 
For each rescue clause in the begin block, Ruby compares the raised Exception against each 

of the parameters of the rescue block one by one. 
The correct match is the exception named in the rescue clause is the same as the t