Wednesday 31 May 2017

New await, async for Javascript.  By creating a function as async we can now await any promises so that the next line will not get executed until the await statement has been resolved.  Below is an example:

async function read () {
 let html = await getRandomArticle(); //We stop here until the article is retrieved.
 let md = hget(html, {
    markdown: true,
    root: 'main',
    ignore: '.at-subscribe,.mm-comments,.de-sidebar'
 });
 var txt = marked(md, { renderer: new Term() });
 console.log(txt);
}

React Native

Run react native packager with simulator for ios:

make platform=ios sim

For Android apps:

make planform=android sim

To map network ports for Android studio:

adb reverse tcp:9080 tcp:9080

Wednesday 12 April 2017

Ruby

Detect returns the first item in the list for which the block returns TRUE. Example:

>> [1,2,3,4,5,6,7].detect { |x| x.between?(3,4) }
=> 3

Returns 3 because that is the first item in the list that returns TRUE for the expression x.between?(3,4).

detect stops iterating after the condition returns true for the first time. select will iterate until the end of the input list is reached and returns all of the items where the block returned true.

Friday 30 March 2017

Ruby on Rails

Use find_by in controllers to find a relevant Active Record, will take a hash and find by multiple params, won’t break with nils and blanks. Click here for further info.

Thursday 29 March 2017

Ruby on Rails

Concerns can be used to share functionality between models.  I had been using module includes  in a lib directory to do this previously, and this is a better way of achieving the same result using an inbuilt Rails mechanism.  There is a great introduction to this concept by Vaidehi Joshi here.

Modules can be extended and included so what’s the difference?   Well extending a module is used when when we want to use extend class methods i.e. “def self.myMyClassMethod” ones.  I found this page useful for illustrating the differences between them and how they can be used.

You can use the included method as a callback so whenever the module is included in another class this callback will be triggered. Here is an example of this use.

Another cool things about the extends keyword is that you can use to extend a particular instance object without affecting any other instances.  For example:

irb(main):001:0> module Greeter; def goodbye; "Bye!"; end; end
=> nil
irb(main):002:0> String.extend(Greeter)
=> String
irb(main):003:0> String.goodbye
=> "Bye!"
irb(main):004:0> x = "foo"
=> "foo"
irb(main):005:0> x.goodbye
NoMethodError: undefined method `goodbye' for "foo":String
        from (irb):5
        from :0
irb(main):006:0>