Archive for the ‘technology’ Category

Ruby Metaprogramming

Ruby is definitely one of the most widely used languages nowadays. And if you are also working on Ruby, you must have heard the word ‘metaprogramming’. Even if you have used metaprogramming, there are chances that you didn’t understand it completely. Metaprogramming is an important concept of Ruby, and you need to understand the usefulness of this concept. the-black-magic-of-ruby-metaprogramming-1-638

What is ‘metaprogramming’?

It is a technique in which the written code writes itself, you can say programming of programming. This might sound confusing, but it is pretty simple to understand and work with. It helps you to add, edit, or modify the code while it is running. It can be used to create new methods or delete the existing ones. One can also reopen or modify existing classes, catch methods that do not exist. Repetitive coding can also be avoided to keep your program DRY.  

How ‘Ruby’ calls methods?

To understand the concept and scope of metaprogramming, it is important to understand how Ruby find a method when it is called. When a method is called in Ruby, it located that method within the code including the inheritance chain.
  1. class Employee
  2.  def test
  3.    “hello”
  4.  end
  5. end
  • charlie_will = Employee.new
  • charlie_will.test # => “hello”
  The test method is called in the example, Ruby first looks for the parent of the charlie.will object because it is an instance of the Employee class, and it has available a method called test, so this method is called. Things get more complicated however when the object is an instance of a class which has inherited from another class:  
  1. class Animal
  2.  def eats
  3.    “food”
  4.  end
  5.  def lives_in
  6.    “the wild”
  7.  end
  8. end
  9. class Pig < Animal
  10.  def lives_in
  11.    “farm”
  12.  end
  13. end
  14. babe = Pig.new
  15. babe.lives_in # => “farm”
  16. babe.eats # => “food”
  17. babe.thisdoesnotexist # => NoMethodError: undefined method `thisdoesnotexist’ for #<Pig:0x16a53c8>
In the above example, mixed inheritance is introduced. Methods defined higher in the inheritance chain needs to be considered by Ruby. When babe.lives_in is called Ruby first check the Pig class for the method, and it is called because it exists in the Pig class.
But if babe.eats method is called the story would be different. Ruby first checks the Pig class for eats method, as it does not exist in it then ‘Animal’ class will be called. In our case, it will respond as eats method exist in Animal class, so it will be called. The method babe.thisdoesnotexist will throw an exception NoMethodError because the method does not exist. It means method defined in the lowest level of inheritance chain will be called if the method doesn’t exist, and an exception will be raised.  

After going through the example, we have discovered that Ruby looks up each method as follows:

  1. Ask object’s parent class if it can respond to the method, call it if found.
  2. If it does not respond, call the next parent class, continue until you reach the top of the inheritance chain.
  3. If nothing in the inheritance chain responds to the method call, the method does not exist.
 

Introducing Singleton Method

Singleton class is designed to give you the hold on to Object Oriented programming. It allows you to create an object that inherits from other classes and call their methods. It specifically does it for a single object. It is also known as Eigenclass.
  1. greeting = “Hello World”
  2. def greeting.greet
  3.  self
  4. end
  • greeting.greet # => “Hello World”
On different thing you would notice here is ‘self’. The greeting.greet method has access to the entire object it has been attached to; in Ruby, it will be referred to that object as ‘self’. In this case, it refers to the String value we attached to it. If we had attached it to anything else, it would have returned that object.
greeting = “I like cheese”
  1. class << greeting
  2.  def greet
  3.    “hello! ” + self
  4.  end
  5. end
  • greeting.greet # => “hello! I like cheese”
Singleton class method allows you to add many methods at once without prefixing all the method names. This syntax also allows you to add anything you would add while declaring the class including attr_writer, attr_reader, and attr_accessor methods.

How does it work?

When you work on the singleton class, Ruby needs a way to add methods to the object we are adding to, something that language doesn’t allow. To do so it creates a new class which is called ‘singleton class’. This class is, made the parent of the object we are working on, given the method and changes instead. It is also made an instance of the previous parent of our object so that the inheritance chain remains unchanged:

some object instance > singleton class > parent class > … > Object/BasicObject

Singleton class add one more step to the Ruby lookup method. Now Ruby looks up in the following way-
  1. Ask the object if its singleton class can respond to the method, calling it if found.
  2. Ask object’s parent class if it can respond to the method, call it if found.
  3. If it does not respond, call the next parent class, continue until you reach the top of the inheritance chain.
  4. If nothing in the inheritance chain responds to the method call, the method does not exist.
Not only can objects gain methods from their inherited classes, but now they can also gain individually unique methods as the program in running.

Putting metaprogramming to work with instance_eval and class_eval

Singleton classes are definitely helpful, but if you want to work with objects dynamically you need to be able to re-open them at runtime within other functions. Ruby does not allow you to have any class statements within a function syntactically, but instance-eval helps to do this. This method is defined in standard Kernel module of Ruby and allows you to add instance methods to an object just like our singleton class syntax.
  1. foo = “bar”
  2. foo.instance_eval do
  3.  def hi
  4.    “you smell”
  5.  end
  6. end
  • foo.hi # => “you smell”
The instance_eval method can take a block to evaluate, which can be used to define new methods as if you are writing a class. These methods will be added to the singleton class of the object.
The methods defined by instance_eval will be insurance methods. You cannot add attr_accessor to an instance method; to do so you need to operate on the class of the object instead of using class_eval:  
  1. bar = “foo”
  2. bar.class.class_eval do
  3.  def hello
  4.    “I can smell you from here”
  5.  end
  6. end
  • bar.hello # => “I can smell you from here”
Although instance_eval and class_eval are quite similar, their scope and application are slightly different. But you can remember which one to use in each situation by keeping in mind that instance methods are made by instance_eval while class methods are made by class_eval.  

Conclusion:

After reading all this, you must be still wondering what and where is metaprogramming? Coming back to the point, metaprogramming allows you to create more flexible code, be it through beautiful APIs or easily testable code. Moreover, it also allows you to do that using powerful programming techniques and syntax. IT allows you to create DRY, highly reusable and extremely concise code.  

App Advertising Monetization Models

The mobile advertising market is on its top game at this point in time. Simultaneously, many new mobile ad companies are introducing themselves. And every company has different monetization model. These models can be easily integrated into applications with their SDK. Let us check out these mobile ad monetization models- 201408-Revenue-models

Offer Walls

This model is generally used in games where the user buys virtual goods for real currency. The Offer Wall enhances user engagement with incentive offers as an integral yet non-intrusive part of the user experience. It gives the player a chance to earn virtual goods by performing different actions on the wall. For example, a user gets virtual sign up bonus to sign up into an app. StartApp, Fiksu, and TapJoy offer this model. Image result for offer wall advertising

Reward Ads

As the name says, this kind of ad model giveaway rewards at different levels. The reward is triggered by some event in the app like crossing a level or getting an achievement. A pop up comes on the user screen through which they will get a discount, gift card, coupon, etc. It is a win-win situation for everyone involved. The user will get a reward, and the owner of the app will also get compensated. Pokkt and Kiip are the lead players of this model. Image result for reward advertising

Banner Ads

Most of the people think that it is one of the most effective ways of advertising, whereas the reality is it is an ineffective method. This kind of ads occupies the top or bottom part of the app screen. When the user taps on the ad, different options open up from watching a video to downloading an app. The dominant players for banner ads are Google’s AdMob and Chartboost. Image result for banner advertising

Interstitial Ads

These ads show up at the transition points in an app. Generally, right before the user starts an app or after a game level is completed. Such ads drop off the user to advertiser’s desired URL. AdMob, TapJoy, Unity Ads use this model the most. Image result for interstitial mobile advertising

Native Advertising

Native advertising is basically content based advertising. The content of the particular website or app is shown in the apps’ news feed. This ad model is considered as the most effective way to generate revenue through mobile advertising. Facebook was the first one to adopt native advertising, and show tremendous growth. AdRoll, ShareThrough, and PubNative offer this model. Image result for native mobile advertising

Notification Ads

The notification ads show up on the screen even when the user is active or working on the app which makes it one of the most undesirable model. This model is only supported by Android. Advertiser will be able to push notification ads at any time. AirPush and LeadBolt work with this mechanism. Image result for notification mobile advertising If you are planning to develop an app that includes mobile advertising, you should research and select the different ad models, then design the app to support the model.  

Android O Features Every Developer Should Know

Google has launched the third developer preview build with Android O for Android app developers. It has been confirmed that the Android version will be Android 8.0. In the third preview, final Android O APIs and various alluring features were released. The features are categorized in two major categories viz. Fluid experiences and Android Vitals. Let us have a look at them- Android-O-Logo

FLUID EXPERIENCES

Fluid experiences are majorly launched for improving the UX. These features will help the Android app developers to offer better user experience. Check out the features that can be used for mobile app development:

#1 Picture in Picture

This feature allows the user to perform multiple tasks simultaneously, like in smart televisions. In this feature, the screen will be divided into two halves which mean you can play a video and do another task simultaneously. For including this feature in your project, you have to specify the user behavior while switching apps. If one of the apps include video, you have to change the code to handle playback, else updating is not required.

#2 Smart text selection

Smart text selection is the result of Google AI. It will help people to perform tasks related to text selection. It will make the selection task much easier, one need not select the whole content. When you will tap on a phrase, it will be selected automatically. This feature will work for all apps, by default.

#3 Autofill framework

Autofill framework is a boon for e-commerce Android app developer. It will enable the app to pull from user data and suggest for different form fields while will make the login process easier and faster. This feature is also available by default, but the performance can be optimized by the developers.

#4 TensorFlow Lite Google

It is a special version of an open source TensorFlow software library that helps mobile apps remain fast and small with machine learning. Image result for android O

ANDROID VITALS

In Android O, features associated with security of Android app marketplace and optimization of app performance are under this category. Some of the features are as follow:

#1 Google Play Protect

With the introduction of this feature, no app will go on Google play store without examining its security. The apps will be scanned every day to overcome threats. Android O will run escalating security checks even on the unofficial app store. It will allow the users to check for malicious apps and even search for a lost phone.

#2 OS Optimizations

Major improvements in Android O includes improvement in boot time of Android devices, memory management, battery consumption, and overall performance. The boot time of Android O on Pixel is two times faster than Android N. Though there are limitations such as background executions, location sensing and scanning for wifi networks.

#3 Android vitals dashboard

Google has launched the dashboard on Play console which will help the developers to check their app’s performance, identify issues and overcome them. It will also play a major role in app optimization and to grow the user base.

#4 Android Go

It is a new OS for low-end devices which is a lightweight version of Android O, designed for the smartphones with 1 GB or less RAM. The apps available on Play Store will also be optimized to run on low memory devices.

#5 Instant Apps

Lightweight apps can be published with this feature. It will also allow users to experience app without downloading and installing them. In addition, it will also help the developers to drive revenue without a download.

#6 Dots Notification

This feature is a total savage for some users as it will display notification badges to show pending notifications without opening the app. These are some of the amazing features of Android O. You can also share your knowledge and experience with us by commenting below. We would love to hear from you.

5 Signs That Show it is the Time to Redesign your Website!

There are very few people who like changes. Changes are difficult, but they are important. Talking about the changes, let us talk about redesigning a website. Redesigning a website is like revamping your whole work portfolio. Your clients will not say to you that your website is old, and you should redesign it. Then how would you know if you need redesigning? Well, there are signs which you can see clearly. You may try to ignore those signs, but you also know that you have to address the issues. So, check out the signs which show that your website needs redesigning:

#1 It has been ages since you redesigned it.1

If it has been more than one year, then it is high-time to revamp your website. The technology is changing every day, so you also need to change or adapt it.

#2 The website is not responsive.3

As I said, the technology is changing on the daily basis that is why responsive websites have become too mainstream. Not having a responsive website means indirectly losing your customers. The websites which are not responsive, do not work on all platforms and devices.

#3 The bounce rate of your website increases.

2 The high bounce rate means non-interactiveness of your website. The reason could be the user interface, the theme or may be the design.

#4 It takes forever to load a page.

4 Page loading is one of the most important things. According to a survey, the user hardly waits more 2-3 seconds for loading a web page. If the page does get loaded the user simply leaves the website, which also increases the bounce rate.

#5 Your website uses flash.5

  Flash was an age old technology. If you are still using flash player, then you are living in the old world. iPhones and iPads do not support it, and also the websites using Flash are not index-able in the search engines. So, these are the reasons why re-designing is important. It is not just for the users, but it is important for business purposes. The world in flowing with the wave of digitisation, and you should also try it.

Code Generation with Bake all in CakePHP

Each MVC module contains a Model, a Controller and multiple views for each table in the application. It can be a big pain to write all the controllers, models and views for your application from scratch and define the relationships. Not only it’s time consuming, banal, it is prone to errors in defining relationships and constraints. To make life easy its time to use Cake’s scaffolding console for code generation, known as Bake. Launching Bake: For Baking, we will have to use PHP executable. Rather than typing PHP path each time, I prefer to include php executable path to the environment variable paths, for comfort rather than necessity. For windows, it’s accessible via -> Right click on “My Computer”->Properties. -> Go to “Advanced“ tab. Click on environment variables. -> Go to system variable path and edit it. -> Add PHP path (Mine is xampp E:/xampp/php) and cake console (E:/xampp/htdocs/cake/console) paths, separated by semicolons. You might need to restart system after adding these paths. Sometimes the bake command doesnt work on windows even after changing environment variables. Make sure you use the correct forward slash for this when giving path for console and php. Sample Path post editing should look like C:Documents and SettingsEnbake>path PATH=C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Program FilesTortoiseSVNbin;E:/xampp/htdocs/campaign3/cake/console;E:/xampp/php;

Sample PATH variable

Time for pre heat Go to the app folder of your application through console. Type “cake bake”. This will give you following screen.  We have two options here:
  1. If your database.php is updated with connectivity settings, this will pick existing tables and give you option to bake them. Cakephp Bake Console
  2. But if DB information is missing in databse.php, we can configure it using ‘D’ as an option to bake. It will prompt us for all the settings and create necessary models, views & controllers.
Super Bake : bake all If you are sure about your DB and don’t want to go individual on baking separately for MVC, you can avail option of bake all. Instead of models this time it will ask you for each entity and on pressing the number it will create models, controllers and views. Hope baking will be as simple as walking through the cake & will save you precious time in coding skeleton for controllers, models and views. Especially when doing a feasibility study or brief demo to the client on the app. Optional Enhancements: Over 1.2 the recent released 1.3 bake has had a significant jump, and a number of features and enhancements have been added.
  • New tasks (FixtureTask and TestTask) are added to the main bake menu
  • TemplateTask has been added for use in the shells.
  • All the different bake tasks now can use connections other than default for baking. Using the -connection parameter.
  • Multiple validations on models has been added.
  • Self Associated models using parent_id are now detected. For example if your model is named Thread, a ParentThread and ChildThread association will be created.
  • Baked Tests include as many fixtures as they know about, including plugin detection (plugin detection does not work on PHP4).
Happy Baking !!!