Reserved word Description
BEGIN Code, enclosed in { and }, to run before the program runs.
END Code, enclosed in { and }, to run when the program ends.
alias Creates an alias for an existing method, operator, or global variable.
and Logical operator; same as && except and has lower precedence.
begin Begins a code block or group of statements; closes with end.
break Terminates a while or until loop or a method inside a block.
\case Compares an expression with a matching when clause; closes with end.
class Defines a class; closes with end.
def Defines a method; closes with end.
defined? Determines if a variable, method, super method, or block exists.
do Begins a block and executes code in that block; closes with end.
else Executes if previous conditional, in if, elsif, unless, or when, is not true.
elsif Executes if previous conditional, in if or elsif, is not true.
end Ends a code block (group of statements) starting with begin, def, do, if, etc.
ensure Always executes at block termination; use after last rescue.
false Logical or Boolean false, instance of FalseClass. (See true.)
for Begins a for loop; used with in.
if Executes code block if true. Closes with end.
module Defines a module; closes with end.
next Jumps before a loop's conditional.
nil Empty, uninitialized variable, or invalid, but not the same as zero; object of NilClass.
not Logical operator; same as !.
or Logical operator; same as || except or has lower precedence.
redo Jumps after a loop's conditional.
rescue Evaluates an expression after an exception is raised; used before ensure.
retry Repeats a method call outside of rescue; jumps to top of block (begin) if inside rescue.
return Returns a value from a method or block. May be omitted.
self Current object (invoked by a method).
super Calls method of the same name in the superclass. The superclass is the parent of this class.
then A continuation for if, unless, and when. May be omitted.
true Logical or Boolean true, instance of TrueClass.
undef Makes a method in current class undefined.
unless Executes code block if conditional statement is false.
until Executes code block while conditional statement is false.
when Starts a clause (one or more) under case.
while Executes code while the conditional statement is true.
yield Executes the block passed to the method.
_ _FILE_ _ Name of current source file.
_ _LINE_ _ Number of current line in the current source file.
Thursday, March 19, 2015
Ruby's reserved words
Convert json to html view
json = {"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
syntaxHighlight(json)
Output::
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
syntaxHighlight(json)
Output::
"{
<span class="key">"employees":</span> [
{
<span class="key">"firstName":</span> <span class="string">"John"</span>,
<span class="key">"lastName":</span> <span class="string">"Doe"</span>
},
{
<span class="key">"firstName":</span> <span class="string">"Anna"</span>,
<span class="key">"lastName":</span> <span class="string">"Smith"</span>
},
{
<span class="key">"firstName":</span> <span class="string">"Peter"</span>,
<span class="key">"lastName":</span> <span class="string">"Jones"</span>
}
]
}"
Tuesday, March 17, 2015
OOPs Concepts .
# Object Oriented Programming : OOP is a method of
programming.The main purpose of object oriented programming is that it
simplify the design, coding by providing re-usability and readability
and also debugging of a program is easier.
-Using this it is easy to identify where to add functions and its related data and which function is used to modify data.
-Features of oops :-Using this it is easy to identify where to add functions and its related data and which function is used to modify data.
[1] Object :-Objects is real world entity that is identify by its unique name.Object consist properties(attributes) and behaviours(methods).
For example a car is a object that have properties like colour, brand name etc. and behaviours like it can move forward and backward.
[2] class : Class is template from which object is created that contains variables for storing data and methods to performing operations on these data.Class have collection of similar type objects.
[3] Inheritance : Process of accessing the behaviours of base class by its derived class. Main advantage of inheritance is re-usability of the code.
(i) Single Inheritance : When a single base class is inherited by a single derived class then the inheritance is called as single inheritance.
(ii) Multilevel Inheritance : A base class inherited by derived and this derived class also inherited by its derived class.
For example grandfather's behaviours inherited by father and father's behaviours inherited by son.
(iii) Hierarchical Inheritance : A base class is inherited by more than one derived class.
For example sparrow, peacock, parrot inherits behaviours of bird class.
(iv) Hybrid Inheritance : Combination of all type of inheritance.
(v) multiple Inheritance : If a derived class inherits properties of more than one base class.
For example duck can swim and also fly.
-Ruby does not support multiple inheritance because it produce a conflict. For example if both the base class have same name then this will produce a confusion.
-So in ruby we can achieve multiple inheritance using module. Module provides a mixin facility. In mixin we can create modules that contains method and we can include more than one modules into our class and we can call modules method using object of class.
[4] Abstraction : Abstraction is the process of hiding implementation and showing only functionality to user.
For example Remote is a interface between user and TV.Using this we can on/off TV but we don't how to circuit works inside remote.
[5]
Abstract class : Abstract class is class that has undefined "abstract"
methods, which are left for subclasses to define. Abstract method does
not have implementation.
For example each bike has a start methods but may be a different technique for different bike for start. Some bike starts by kick and some starts by pressing button.
So we can declare start method as a abstract method and we can implements it in derived class for new bike.
[6] Encapsulation : Encapsulation is a process of binding data and methods together.Encapsulation protect data from unwanted access or alteration by making it private.
Encapsulation = Abstraction + Data Hiding.
[7] Polymorphism : Different behaviours at different instances depend upon the data passed in the operation. Polymorphism increase code readability.
For example a software engineer performs many task such as sometimes he performs coding, sometimes he performs testing, sometimes he performs analysis. So software engineer is one but he is performing different task depending upon requirement.
There are two types of polymorphism.
(i) Compile time polymorphism : It is achieved by method overloading. In this, call to a overloaded method is decide at compile time.
(ii)Runtime Polymorphism : It is achieved by method overriding. In this, call to a method is decide at runtime.
For example each bike has a start methods but may be a different technique for different bike for start. Some bike starts by kick and some starts by pressing button.
So we can declare start method as a abstract method and we can implements it in derived class for new bike.
[6] Encapsulation : Encapsulation is a process of binding data and methods together.Encapsulation protect data from unwanted access or alteration by making it private.
Encapsulation = Abstraction + Data Hiding.
[7] Polymorphism : Different behaviours at different instances depend upon the data passed in the operation. Polymorphism increase code readability.
For example a software engineer performs many task such as sometimes he performs coding, sometimes he performs testing, sometimes he performs analysis. So software engineer is one but he is performing different task depending upon requirement.
There are two types of polymorphism.
(i) Compile time polymorphism : It is achieved by method overloading. In this, call to a overloaded method is decide at compile time.
(ii)Runtime Polymorphism : It is achieved by method overriding. In this, call to a method is decide at runtime.
Sunday, March 15, 2015
Difference between link_to, redirect_to, and render
# link_to is used in your view, and generates html code for a link
<%= link_to "Google", "http://google.com" %>
This will generate in your view the following html
<a href="http://google.com">Google</a>
# redirect_to and render are used in your controller to reply to a request. redirect_to will simply redirect the request to a new URL, if in your controller you add
redirect_to "http://google.com"
anyone accessing your page will effectively be redirected to Google
# render can be used in many ways, but it's mainly used to render your html views.
render "article/show"
This will render the view "app/views/article/show.html.erb"
The following link will explain the redirect_to and the render methods more in detail http://guides.rubyonrails.org/layouts_and_rendering.html
<%= link_to "Google", "http://google.com" %>
This will generate in your view the following html
<a href="http://google.com">Google</a>
# redirect_to and render are used in your controller to reply to a request. redirect_to will simply redirect the request to a new URL, if in your controller you add
redirect_to "http://google.com"
anyone accessing your page will effectively be redirected to Google
# render can be used in many ways, but it's mainly used to render your html views.
render "article/show"
This will render the view "app/views/article/show.html.erb"
The following link will explain the redirect_to and the render methods more in detail http://guides.rubyonrails.org/layouts_and_rendering.html
Monday, March 2, 2015
Database dump from heroku ( pgbackups )
heroku pgbackups:capture --expire --app app_name
heroku pgbackups:url --app app_name
curl -o file_name.dump "url"
heroku pgbackups:url --app app_name
curl -o file_name.dump "url"
Friday, February 27, 2015
RESTful architecture
RESTful interface means clean URLs, less code, CRUD interface.
CRUD means Create-READ-UPDATE-DESTROY.
You might heard about HTTP verbs, GET, POST. In REST, they add 2 new verbs, i.e, PUT, DELETE.
There are 7 default actions, those are – index, show, new, create, edit, update, destroy
GET is used when you retrieve data from database.
POST is used when you create new record in database.
PUT is used when you are updating any existing record in database.
DELETE is used when you are destroying any record in database.
Following table may clear the concept.
Action VERB
index GET
show GET
new GET
create POST
edit GET
update PUT
destroy DELETE
CRUD means Create-READ-UPDATE-DESTROY.
You might heard about HTTP verbs, GET, POST. In REST, they add 2 new verbs, i.e, PUT, DELETE.
There are 7 default actions, those are – index, show, new, create, edit, update, destroy
GET is used when you retrieve data from database.
POST is used when you create new record in database.
PUT is used when you are updating any existing record in database.
DELETE is used when you are destroying any record in database.
Following table may clear the concept.
Action VERB
index GET
show GET
new GET
create POST
edit GET
update PUT
destroy DELETE
Tuesday, February 24, 2015
The around_* callback is called around the action and inside the before_* and after_* actions. For example:
class User
def before_save
puts 'before save'
end
def after_save
puts 'after_save'
end
def around_save
puts 'in around save'
yield
puts 'out around save'
end
end
User.save
before save
in around save
out around save
after_save
=> true
Subscribe to:
Posts (Atom)
FastAPI: The Modern Python Web Framework
Introduction to FastAPI: The Modern Python Web Framework The world of web frameworks has always been competitive — from Django and Flask ...
-
# Object Oriented Programming : OOP is a method of programming.The main purpose of object oriented programming is that it simplify the ...
-
rspec is a full-featured testing framework that will let you write what Rails considers unit tests, functional tests, and integration test...
-
Cucumber – a Behavior Driven Development (BDD) framework which is used with Selenium for performing acceptance testing. Cucumber Introd...