Magical ‘If..then..else’ conditional statement

I believe whole programming world stands on couple of pillars and ‘if..then..else’ (ITE) statement is one of those. Everyone programmer and non-programmer understands ITE conditional statement for sure.

 A typical if..then..else.. statement in Ruby:

 if 1 == 2
   puts "inside if statement"
 else
   puts "inside else statement"
 end
1.9.3p385  > inside else statement
Another example in ruby:
#/bin/ruby
near = "father"
if near == "father"
  puts "I'm studying"
elsif near == "mother"
 puts "I'm playing"
else
 puts "I'm sleeping"
end

1.9.3p385  > I'm studying

Essence is either of two given block is executed i.e. either if block or else block is executed by the code in any language.

Can there be a case/situation when both of these blocks if & else are executed?

…. This is really an unusual question but seriously can this be achieved. Can we challenge the very existence of conditional flow? Are the basics we have learned aren’t true anymore?

Answer lies in situation and yes this can be achieved. Unix fork(2) is the answer and can help if..then..else to behave in this manner.

example magicalif.rb

if f1 = fork
  puts "inside fork"
elsif f2 = fork
  puts "inside second fork"
else
  puts "outside fork"
end

─$ ruby magicalif.rb

inside fork
inside second fork
outside fork

So what has just happened? Condition checks for fork(2), kernel command to create a subprocess of the parent process.  So fork, creates a subprocess called child process similar to parent process (having copy of parent process, file descriptors and sockets etc) and returns a pid (child process) to parent process and nil to child process. Hence, IF block was executed by parent process as it returned pid to parent process and ELSE statement is executed by CHILD process as fork returned nil to child process.

Let’s have a closer look which block is executed by which process (parent or child)

supermagicalif.rb

puts "Master Process: #{Process.pid}"
if f1 = fork
  puts "inside fork"
  puts "Fork PID: #{f1}"
  puts "Process pid: #{Process.pid}"
elsif f2 = fork
  puts "inside second fork"
  puts "Fork PID: #{f2}"
  puts "Process pid: #{Process.pid}"
else
  puts "outside fork"
  puts "Process pid: #{Process.pid}"
end

─$ ruby supermagicalif.rb
Master Process: 21267
inside fork
Fork PID: 21268
Process pid: 21267
inside second fork
Fork PID: 21269
Process pid: 21268
outside fork
Process pid: 21269

This clearly shows IF block is executed by PARENT process, ELSIF block executed by first CHILD process and ELSE block executed by SECOND CHILD process.

Will have a more closer look in fork(2) in next blog where will see it’s usage and how can it be used in our programming. For now, yes it’s possible and now you know MAGICAL IF..THEN..ELSE statement 🙂

No to apache/nginx configs. zero-config Rack server for Mac OS X

Are you developing Rails application from a while? I’d be interested to hear how do you run it locally especially on Mac OS X.

Possible options are:

My preferred choice is phusion passenger for Apache or Nginx and writing VirtualHost config everytime I create a new Rails application. Listen to new port and set the virtual host and restart apache or nginx. Boom. Done.

Else it’s good to start Mongrel or webrick instances if Apache/Nginx is down and it’s up in a minute or when Virtual Host port and configs start becoming confusing.

Recently I got to install POW.CX

Pow is a zero-config Rack server for Mac OS X. Have it serving your apps locally in under a minute.

Install it using

$ curl get.pow.cx | sh

To set up a Rack app, just symlink it into:

$ cd ~/.pow $ ln -s /path/to/myapp

 

That’s it! Your app will be up and running at http://myapp.dev/. See the user’s manual for more information.

Also one can use powder which manages POW and makes easy pow more easier .

https://github.com/Rodreegez/powder

It’s simple, straight and offers Convention over Configuration. No more configs or running mongrel instances to start an application.

How to study and learn from world’s best Teachers and Professors

When I was in college their was always a desire that If I would have been in Stanford or some great university I would have access to world’s best professors and it would have been a different learning experience altogether.

Or

How great it would be if I can study whatever I feel like outside of our course in a systematic way and can learn the fundamentals that help to grow my knowledge, fundamentals or businesses

Education for all

Education for all

Internet is open and google always helps but still felt their is a lack to find the best course material or community for International Students to learn new courses and subjects that interests you. Life is no better and still lakhs of student who passes out every year have very diminished knowledge about what they study. In most part, Indian Universities and Institutions are unable to provide good Professors or teachers to students. It still continues to be like this and “Quality Education” is still far from anyone without any differences. Students are first categorized on basis of their caste, creed, grades and reservation. Economic condition of a family is always a primary concern besides all these factors.

Recently after 4 years of college education and during my visit Bay area and Stanford. I heard about Coursera, founded by Stanford computer science professors Daphne Koller and Andrew Ng ( StanfordDaily ).

About Coursera

We offer high quality courses from the top universities, for free to everyone. We currently host courses from Princeton University, Stanford University, University of California, Berkeley, University of Michigan-Ann Arbor, and University of Pennsylvania. We are changing the face of education globally, and we invite you to join us.

Then I started with more exploration and soon realized some best part of joining these courses. Mind it it’s free

  • Variety of Courses. I can study about anything and take up course of my choice e.g Algorithms, Pharmacology, Finance, Humanitics etc
  • Course taught by World’s best professors and Universities. Seriously it feels great to have access to best profs.
  • Small Video based tutorial. According to me “Small” videos are the key, as it’s easy to concentrate for shorter time and easy to watch as and when time suits you
  • Interactions with like minded people from the globe on the Discussion Forums
  • Format of the Syllabus and weekly assignment keep the clock ticking
  • Content of the course is way too good and easy to understand for anyone.

Last but not the least, now anyone from anywhere in the world can enroll and (re)study without any hesitation in his/her own time.

Heartiest thanks to the Universities and their Teachers and Professors to impart their knowledge to the world from their schedule and thanks to Coursera for providing it for free without any difference to everyone. Period

Note: This is in no way Coursera marketing but genuine feedback from a learning student 🙂

Happy Learning!

Online vs offline commerce: Targeting and Re-targeting your customers

Attended Triggr event today at InstaMedia Noida. It was interesting to hear couple of presentations and thoughts which was majorly focussed on one term “ENGAGEMENT”. I started loving this term especially, as everything revolves around this one simple term. Either your business is online or offline, engagement is involved at first hand once user showed some interest, then comes all other factors. If you succeeds in this engagement, you can turn your business to X folds.

In offline market, you go to shopkeeper to buy something, you showed interest then he starts engaging or recommending things to you, as you might also be interest and we tend to buy most of the times (credibility plays a vital role). If something is unavailable or when you come next time, he remembers what you want, he’ll greet you and engages you more. Now you’ll go to that shopkeeper again and again. Thats how a typical offline businesses run.

offline shopkeeper engagement

Talking in terms of Online context, their is no physical person, companies usually do lot of investment on Suggestions, Recommendations of products if a person shows a interest by saying “people also bought this”, “we recommend you to buy this” etc. to engage the users and converting it into business.

online product recommendations

Their were talks on how to minimize the bounce rates and more user engagement. Some implemented and proven ideas are designing it right way which comprises focusing the social media aspect i.e fb/tw views etc (talk from Slideshare), a/b testing (Paras, http://visualwebsiteoptimizer.com), Multi Variant testing (MVT), landing pages ( I must say unbounce guys have done a fantastic job) to understand what works best in terms of user engagement. Your product should ideally deliver the right message about your product in minimum words and should emphasize on self-talking images and help user talking the action on your page (without confusion). Once done, he will spend time to understand your product(s).

Interesting, met with Nitin Chawdhary, India Head, Tyroo ad network, I really like his presentation on E-Commerce, Engagement and Re-targeting your customers. To explain re-targeting in layman’s terms:

If your customer shows some interest in a product but goes off, next time if you happen to advertise/target him again, about that very specific items he was interested in, there is a high probability that he will buy this time.

According to him, if you re-target your customer around 5-6 times, there are high chances of actual lead conversion. One of the pain points he mentioned in e-commerce is, its very hard to find and target the customer back, once he is gone.

Talking more in terms of offline businesses or online businesses and once you scale re-targeting is hard to achieve, I’m sure technology has its limitations but its achievable on Internet. But re-targeting in physical world is yet missing according to me and in Indian context e.g. If I have an intention to buy a microwave and I go to market and try out couple of stores, how about the store shoots a message back to me (once I left his store) with some offer, to actually convert the lead and the time sales guy invested in explaining the whole Microwave functionality in person. I think enterprises will be interested in these solutions of re-targeting in physical world and simple implementation of this common and complex problem.

Smartphone Native Application using Ruby in 30 min

Building a Smartphone (Android, iPhone, BlackBerry etc) app using ruby

Google Android
iPhone Logo
BlackBerry Logo


RhoMobile Framework


Rhodes

Rhodes is an open source framework to rapidly build NATIVE apps for all major smartphone operating systems (iPhone, Windows Mobile, RIM, Symbian and Android). These are true native device applications (NOT mobile web apps) which work with synchronized local data and take advantage of device capabilities such as GPS, PIM contacts and camera.

Yeah! Its in Ruby 🙂

  • support for ALL smartphones
  • synchronized offline data
  • a Model View Controller (MVC) framework
  • an Object Relational Manager (ORM) to avoid direct SQL coding
  • the mobile Ruby
  • a hosted development service
  • scaffolding to quickly build the app

Setup (Android)

  • gem install rhodes
  • Java JDK latest version is needed for Android. Make sure its installed. Test by typing $javac -version in your terminal
  • Download Android SDK (for linuxMac OSWindows) and extract at a convenient location.
  • Change in your ~/.bash_profile(Mac) and ~/.bashrc(Linux) to add
    export PATH=/path-to-android/tools:$PATH
    export ANDROID_HOME=/path-to-android
  • typing $android in the terminal shall start Android SDK and AVD Manager
  • once ACD manager started install the latest packages under “Available packages”, it will take a while to install
  • Once installed they will list under “Installed Packages”
  • else installation follow these instructions to install Android SDK starter packages, and change the PATH environment variable for sure
  • Download and install Android NDK (Win, Mac OS, Linux) and export your path same as above

Setting up rhodes
Now we need to link our rhodes installation to the SDK and eclipse.
$rhodes-setup
You’ll be prompted with questions – my answers are below:
  • JDK path: /Library/Java/Home
  • Android SDK path: /opt/androidSDK/
  • Android NDK path: /opt/androidNDK/
  • the remainder of the questions related to Windows and Blackberry: (left blank)

HelloWorld Program

rhogen app HelloWorld

cd HelloWorld

rake -T  (will list all the rake tasks for all supported smartphones)

rake run:android (it will build, install and launch the HelloWorld Program)

Of course it will be blank and its just the start. Go to launcher you will be able to see your HelloWorld application with rhodes default icon.

My Contacts

This is fine, but what about ruby and MVC framework? Open your latest HelloWorld in your favorite editor(textmate etc). You will find an “app”, “app=>helpers” folder much like rails “rhoconfig.txt” => configuration settings

$ rhogen model Contact name, email, phone

Generating with model generator:

[ADDED]  app/Contact/index.erb

[ADDED]  app/Contact/edit.erb

[ADDED]  app/Contact/new.erb

[ADDED]  app/Contact/show.erb

[ADDED]  app/Contact/index.bb.erb

[ADDED]  app/Contact/edit.bb.erb

[ADDED]  app/Contact/new.bb.erb

[ADDED]  app/Contact/show.bb.erb

[ADDED]  app/Contact/contact_controller.rb

[ADDED]  app/Contact/contact.rb

[ADDED]  app/test/contact_spec.rb

You can see the controller, model and view files similar to Rails frame work.

Change the

start_path = ‘/app/Contact’ (in rhoconfig.txt)

$ rake run:android

Your application will relaunch and you can click “new” button to add new contacts and edit and delete them. So basic CRUD functionality is available much like rails and ready to go.

I hope you will find this ready to go tutorial very helpful to start of your application on any smartphone using Rhodes framework.

Stay tuned for more!