Pry is one of the great tool for debugging ruby based application. In this blog would like to show some of the cool features of the Pry.
Friday, January 19, 2018
Ruby - Array#bsearch
Array bsearch it's very handy and one of the fastest way to find a value from the array.
Let's do benchmark analysis, based on the result, bsearch more powerful than using array find method.
It can find a match with only O(log n) complexity.
Let's do benchmark analysis, based on the result, bsearch more powerful than using array find method.
require 'benchmark'
array_data = (0..100_000_000)
Benchmark.bm do |x|
x.report(:find) { array_data.find {|number| number > 50_000_000 } }
x.report(:bsearch) { array_data.bsearch {|number| number > 50_000_000 } }
end
user system total real
find 3.160000 0.000000 3.160000 ( 3.174570)
bsearch 0.000000 0.000000 0.000000 ( 0.000011)
It can find a match with only O(log n) complexity.
Labels:
binary search,
Ruby
Tuesday, October 31, 2017
Node.js Best Practices - Async
Async function allows you to write Promise based code as if it were synchronous. It was shipped with V8 Engine since the Version 7.6.
Define a function using the async keyword, then you can use the await keyword within the function's body.
Async function is called, it returns with a Promise. When it returns a value, the promise gets fulfilled.
Async function throws an error, if it's rejected
Simple Async function
If your applications are using Promise, then you can use "await" your Promises, instead of chaining them.
Above example leverage Promise using Node.js build-in "util.promisify" and read the file asynchronously.
Promise Reject/Error Handling
In the newer version of Node.js, if Promise gets rejected it will bubble down to the Node.js process. Make sure you handled the "try-catch" block properly on your app.
Define a function using the async keyword, then you can use the await keyword within the function's body.
async function app () {
...
await ...
}
Async function is called, it returns with a Promise. When it returns a value, the promise gets fulfilled.
Async function throws an error, if it's rejected
Simple Async function
const util = require('util')
const {file} = require('fs')
const fileAsync = util.promisify(readFile)
async function app () {
const content = await fileAsync('log')
return content
}
app()
.then(console.log)
.catch(console.error)
If your applications are using Promise, then you can use "await" your Promises, instead of chaining them.
Above example leverage Promise using Node.js build-in "util.promisify" and read the file asynchronously.
Promise Reject/Error Handling
In the newer version of Node.js, if Promise gets rejected it will bubble down to the Node.js process. Make sure you handled the "try-catch" block properly on your app.
const util = require('util')
async function app () {
try {
await new Promise((resolve, reject) => {
reject(new Error('error'))
})
} catch (err) {
// handle the error
}
}
app()
.then(console.log)
.catch(console.error)
Tuesday, October 24, 2017
Debugging Node.js application
Debugging Node.js app is always fun, especially if you're coming from other language background such as Ruby, Python. you may looking similar debugger(binding.pry or pdb) on node js world.
In this article, let me go through how debugging node.js app with Chrome DevTool
1. Install Node.js, i use nvm
2. To start debugging mode, run the Node.js app with inspect flag
3. Open Chrome inspector and click on "Open dedicated DevTools for Node" to debug your app code

Now you can debug your javascript code by adding breakpoint or "debugger"
Happy debugging :)
In this article, let me go through how debugging node.js app with Chrome DevTool
1. Install Node.js, i use nvm
$ nvm use v8.7.0
Now using node v8.7.0 (npm v5.4.2)
2. To start debugging mode, run the Node.js app with inspect flag
node-app $ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/a51c84e5-78c1-481e-ac5b-ab6bc78c23fe
For help see https://nodejs.org/en/docs/inspector
Debugger attached
3. Open Chrome inspector and click on "Open dedicated DevTools for Node" to debug your app code

Now you can debug your javascript code by adding breakpoint or "debugger"
Happy debugging :)
Labels:
Debugging,
JavaScript,
Node.js
Sunday, November 23, 2014
TextMate bundle for remove space, add newline, etc
This is one of the useful bundle for TextMate which converts Tabs to Spaces, Remove Trailing Whitespace and Add a newline at end of file
https://github.com/glennr/uber-glory-tmbundle
Subscribe to:
Posts (Atom)
