There are three ways to group trunk of code in Ruby which are block,
process and lambda. Each of them behave differently from others. This post
is all about the differences of them and its examples and use cases.
Basic usage
Object or not
Block is not an object, cannot execute directly, it need to be passed to a method
Proc and Lambda are objects, its instance can be execute directly
Number of block, proc and lambda passed in parameter field
Block: A method can use only one block which passed in the parameter fields.
Well, obviously, block is a kind of anonymous function. If there are two
passing in parameter field. How could interpreter understand when should it
use this or that block. Meanwhile, a method can only use one block or
anonymous function which is passed in parameter field.
Proc and Lambda: In fact, these two are identical and it’s object
instances. On the other words, you are passing object in parameter field. Hence,
pass them as many as you want.
Return behaviour
Block: return cannot be used within a block. Here is an article about
block break and next behaviours
Lambda: return instruction within a block only suspend instruction execute within
the block. It does not suspend outer method.
Process: return instruction also affect the outer scope. It suspends the
outer method directly.
Checking number of arguments passing to block, process, lambda
During this time, I am in an internship program. There is a big concern, I have
to work with a very big project, and there is no documentation. The number of
tables in project’s database is more than 20, still no documentation.
A big nightmare for an intern one. It’s important to make an entity relationship
diagram, at least with a hope that I can get cope easily with the project.
Configuration file will be loaded at ~/.erdconfig or
...project_rails/.erdconfig, file config in the root of project will take
higher priority than in the home directory.
Generating entity diagram file
Entity diagram will be generated in the root directory of project.
Highchart is a common javascript library which use to display graph with many
beautiful features, however, there is a small need in use of highchart.js. It’s
about having dual yAxis, and how to balance common zero between two yAxis.
There is no built-in feature that can help you deal with this solution but
changing the min, max value via setExtremes() method.
I did write a small method which can help solve this problem, small but
achievable.
github
How to use
Given that, on the html there is a div which is used for highcharts, this div
is only for rendering graph for example $("#graph")
After include the balancezero.js, you can test by running the command on the
web console balanceZeroRoot(dom_element) or for example balanceZeroRoot($("#graph"))
By default, there is exist behavior for show and hide plots, you need to
modify those default behavior to work with balancezero.js. For each kind of
graph, there is exist option name legendItemClick for example
link
In addition, there is also a need to load balanceZeroRoot() right after
data loaded. There are many kind of graph, the code below is an example about
charttype
Note
The demand to have common zero for dual yAxis has been there for 2 years
link
, however there is no good solution which fit for me even the one from the admin
of highcharts, the plugin does not work perfectly.
private variables are declared with the var keyword inside the object, and
can only be accessed by private functions and privileged methods.
private functions are declared inline inside the object’s constructor (or
alternatively may be defined via var functionName=function(){...}) and may
only be called by privileged methods (including the object’s constructor).
privileged methods are declared with this.methodName=function(){...} and
may invoked by code external to the object.
public properties are declared with this.variableName and may be
read/written from outside the object.
public methods are defined by Classname.prototype.methodName =
function(){...} and may be called from outside the object.
prototype properties are defined by Classname.prototype.propertyName = someValue
static properties are defined by Classname.propertyName = someValue
#3. Inheritance
a.Inheritance object
#4. Notes
Keyword this, within a function, this refers to object which call the
function.
This project is a project for hackday in Cogini company in 2014
1. Identify the page mode
2. Declare css selector, relative variables
3. Declare function to make a click action to DOM node
4. Make those function become interactive
5. Reference
I must say thank to Tran Xuan Truong, Quan Bao Thien To. When I write this bunch
of code I don’t know much about javascript. In addition, regarding Conkeror
technical issues, I gained help from Tran Xuan Truong who is a master in Conkeror
Web browser and he is one who has a big love in programming. Thank you, I will
remember the hackday.
Right now, It’s April 14, 2015. I note this memory to remember a day of doing new
things, learning new things and of course, because it’s a memorial hackday.
Even though, currently, the haivl.com has been collapsed,but this project still
work with haivainoi.com
Problem: Apache Web Server announces that Forbidden Error, given that developers configure Allow and Deny directory with no mistake
Reason: A lack of Indexes for files in directory.
Solution: Add option Indexes in directory tag <Directory>
Example: The following configuration causes forbidden error
Problems: Cannot load the fonts of bootstrap
Reason: In Rails, all assets are loaded from host:port/assets not
host:port/fontsSolution: Replace the source url in @font-face, from ../fonts/ to
/assets
There is no need to descript more about the project, the main role of this
project named music-api is to provide a web service for developers to exploit
indirectly the song’s information coming from any online source such as ZingMp3,
Nhaccuatui.
This project is an open source software, developers contribute and developer my
project. This time, this is only two online source ZingMp3 and Nhaccuatui,
however, the number source will be increased regarding developers’ demand.
The song information includes name, singers, lyrics, song's page and
inparticular song's source for downloading.
Why does it work ?
Fundamentally, this service send a request to music oridinary servers, after receiving responses, it analyze the response (inspect elements selectively). After finished analysis, it send json object which inlcluding all song’s information. Currently, the web service is deployed at USA by heroku, Because of IP filter applying by VNG(ZingMp3), there are some songs which you cannot search. I have tested by using VPN locating in the USA, the result is limited.
Good news is that Nhaccuatui is very generous, they allow foreign IP to access all song, meanwhile, you can exploit from more songs.
Request should be sent to http://silverlink.herokuapp.com/api, and server
will return a result in JSON object
there are three parameters to query:
– source: the source of music (zing mp3 = 1, nhaccuatui = 2) - this is compulsory
– keyword: the keyword - this is compulsory
– number: the expect number of song - this is optional. The maximum number of
song user can query from ZingMP3 is 60, and 111 for Nhaccuatui