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