Thursday, February 27, 2014

Ruby: decorators, modules

Computer Programming in 5 minutes & Tiny-Robots

I have wondered several times how I would introduce 'programming' to someone who has never done it, not even a little bit.

Below is Larry Wall's take on it. He is the creator of Perl programming language.



The rest of this post is based on an idea from the above video - the idea of robots being at your disposal to do certain tasks.

We can probably explain programming-constructs, by :

  • asking the student to imagine that each programming-construct is actually a tiny-robot. So, for e.g., a for-loop, is actually a tiny-robot that is capable of just doing a 'loop'. Similarly, we can imagine a if-then-else tiny-robot, print-to-screen tiny-robot, etc
  • Notice that I modified 'robots' to 'tiny robots' so as to convey the idea/feeling that a tiny-robot can do a 'tiny' task
  • This idea might work because everyone knows that robots need to be instructed what to do i.e. robots need to be given 'commands'.
  • And it is also easy to add that the commands have to be very specific, otherwise the robot will not understand what to do. We would have introduced concept of 'syntax' by doing that.

And that is probably half the battle won right at the start.

------------------------------------------------------------------------
More complex point that can be introduced after a couple of programs -

  • We can ask the tiny-robots to give commands to each other. 

------------------------------------------------------------------------
Example:

PROBLEM:
Print numbers 1 to 100 on screen

TINY-ROBOTS AT YOUR DISPOSAL:
1. LOOP tiny-robot
2. PRINT-TO-SCREEN tiny-robot

Info about the LOOP tiny-robot -
obviously since it is a robot, we have to tell it exactly how many times it has to loop;
once we give tiny-robot this number, it stores it and starts looping; it keeps track of how many loops it has completed, how many are remaining, etc; the LOOP tiny-robot is quite smart in this aspect.

Info about PRINT-TO-SCREEN tiny-robot - 
this tiny-robot does nothing but print to screen whatever you give it.

SOLUTION:
Instructions from us (programmer) to the tiny-robots will be:
"LOOP tiny robot -  please loop 100 times"
"During each loop, since you keep track of how many loops you have already done, give that number to the PRINT-ON-SCREEN tiny-robot, and ask it to print that number on screen.
-------------------------------------------------------------------------------
(In the above example, I have tried to tackle the 'loop' construct, which is a slightly advanced concept for an absolute beginner.
Have to try and add IF-THEN-ELSE construct example;
If idea seems good, perhaps, I will try to create examples for all the main programming-constructs.
In the worst-case, it can be a good way for me to try out while trying to understand new concepts)

Wednesday, February 19, 2014

git add -p

http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
If you use git, you've used the command git add. But do you know about git add's "patch mode" using git add -p ?
Patch mode allows you to stage parts of a changed file, instead of the entire file. This allows you to make concise, well-crafted commits that make for an easier to read history.

10-signs-that-you-are-an-awesome-web-developer

Friday, February 14, 2014

Tuesday, February 11, 2014

MySql: find out which tables contain a certain column

To search for a column 'rank' in a database 'mydb':

select * From INFORMATION_SCHEMA.COLUMNS Where column_name like '%rank%' and TABLE_SCHEMA = "mydb"

Monday, February 3, 2014

drive.draw.io - mockup tool

Ruby: yield

Ruby: inject

http://ruby-doc.org/core-2.0/Enumerable.html#method-i-inject

inject( initial )  { |memo, obj|      block  }  → obj

def sum_of_cubes(a, b)
  sum = 0
  (a..b).inject(sum) { |sum, x| sum + (x*x*x) }
end

What does it do ?
-- Combines all elements of enum
-- by applying a binary operation, specified by a block

For each element in enum -
- the block is passed an accumulator value (memo) and the element
- the result becomes the new value for memo.
At the end of the iteration -
the final value of memo is the return value for the method.

Variation:
-------------------------
inject  { |memo, obj|     block  }  → obj

What is the initial value for memo ?
- If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.


Followers

Blog Archive