Wednesday, June 28, 2006

LeapYear V2

And Neo said....'Hmmmm. Upgrades'.

Here is the version 2.0 of the program:

# LeapYearv2.rb - This program reads an year and tells whether it is a LeapYear or not.
# Author - Ashish Kulkarni (http://ashishkulkarni.blogspot.com)
# Date - 28-Jun-2006

GregorianCutoverYear = 1582

puts 'Please input the year for checking for Leap Year:'
STDOUT.flush
year = STDIN.gets.chomp.to_i

if (year >= GregorianCutoverYear && # Gregorian
((year.modulo(4) == 0) &&
((year.modulo(100) != 0) ||
(year.modulo(400) == 0)))) ||
(year < GregorianCutoverYear && # Julian
year.modulo(4) == 0)
puts 'It is a Leap Year'
else
puts 'It is not a Leap Year'
end


Output:

Please input the year for checking for Leap Year:
2000
It is a Leap Year

Please input the year for checking for Leap Year:
2002
It is not a Leap Year

Please input the year for checking for Leap Year:
2004
It is a Leap Year

Please input the year for checking for Leap Year:
2006
It is not a Leap Year

Please input the year for checking for Leap Year:
3000
It is not a Leap Year

LeapYear Assignment

Program:

# LeapYear.rb - This program reads an year and tells whether it is a LeapYear or not.
# Author - Ashish Kulkarni (http://ashishkulkarni.blogspot.com)
# Date - 28-Jun-2006

puts 'Please input the year for checking for Leap Year:'
STDOUT.flush
year = STDIN.gets.chomp.to_i

if year.modulo(4) == 0
if year.modulo(100) == 0
if year.modulo(400) == 0
puts 'It is a Leap Year'
else
puts 'It is not a Leap Year'
end
else
puts 'It is a Leap Year'
end
else
puts 'It is not a Leap Year'
end

Output:

Please input the year for checking for Leap Year:
2000
It is a Leap Year

Please input the year for checking for Leap Year:
2001
It is not a Leap Year

Please input the year for checking for Leap Year:
2003
It is not a Leap Year

Please input the year for checking for Leap Year:
2004
It is a Leap Year

Please input the year for checking for Leap Year:
2008
It is a Leap Year

Please input the year for checking for Leap Year:
3000
It is not a Leap Year

I made use of the Ruby Manual under C:\ruby\doc\ProgrammingRuby.chm to find the modulo() function.

Tuesday, June 27, 2006

The drive to Falkirk

To get a permanent driving license in UK, one needs to do the following:

1. Get a provisional driving license (you can't proceed any further unless you have got this). This is a 2 part document. First part is a plastic card and second part is a paper document.
2. Pass the Driving Theory Test. This has 2 parts. First is a multiple answer section of 35 questions. You have to get atleast 30 of them correct. Second is a section of 14 video clips. You have to click the mouse as soon as you can see a hazard. Once you pass this, you get a theory certificate which remains valid for 2 years.
3. Once you have a provisional driving license, you can start taking driving lessons. Once you have passed the theory test, and your instructor feels that you are ready, you can apply for the practical test. You can give as many practical tests as needed (to pass) before your theory certificate runs out. If that happens, you need to sit your theory test again.
4. The practical test is a one hour drive where the driving examiner sits next to you and asks you to drive around. This takes place not in a test area but on real road and amongst real traffic. They have recently added a bit where they ask you to open the bonnet of the car and ask you a few questions about the stuff under the hood.

My practical test is on the 18th of August 2006. And my driving instructor John has been telling me that I need to get more practice than just the one hour that he takes me out every week.

The irony is that I already have a car which Kate generously bought for me as my birthday gift last year.

So eventually this Sunday, after a long time, I got behind the wheel of my car and drove it around Stirling and Bannockburn. For those who don't know much about Scottish History, Stirling is where the famous battle between Scots and English took place. You can see the results in the movie Braveheart.

Stirling has got the Wallace Monument where William Wallace's sword rests....and also a beautiful castle - The Stirling Castle.

On Sunday, I drove around all these places.

Unfortunately John was off sick on Monday. So Kate decided to take me for a run in the car.

And I drove all the way from Stirling to Falkirk via Camelon and Larbert and came back via another road - over the Kinkardine Bridge.

It was a good drive and I came back via Alloa. I hope to get some more hours behind the wheel.

gets lives to see another day

Found another interesting function - gets

printf "Name: "
STDOUT.flush
name = gets
printf "Hello " + name

Output:

Name: Ashish
Hello Ashish

The journey of discoveries continues.

printf reborn

I was looking for ways to format the Ruby output and lone-behold, bumped into an old friend - printf.

printf "%3.2f\n", 2.0 / 3.0

Output:
0.67
(and a big grin on my face)

The escape character for escaping the newline works only on double quotes and together with printf, gives me the comfort of knowing that a bit of c/Java syntax is still at the heart of Ruby.

Comments in Ruby!!!

Just finished the next few Ruby programs. What did I learn? Well, the first thing is COMMENTS!!!

Single line comments start with #. Put a # anywhere on the line and rest of the line becomes a comment.

Multiline comments start with =begin and end with =end.

It must be noted that /*, */ and // do not work.

Just tried the oracle way and no, -- does not work either.

Semi-colons, whether at the beginning or at the end of the, statement are ignored as well.

I hate to say this but Ruby is a completely new language. So woe is me. No shortcuts. I will have to learn everything from scratch.

And I thought that they were joking when they said life begins at 30 (in my case, 34).

No, I don't feel like Alice in wonderland or Dorothy following the yellow brick road.

I still have more questions.

Yes, I do want to know as to why people start new languages rather than adding to existing ones. But we won't go there yet.

Now that we can output stuff, my next question is how do we read stuff from console into variables?

And how do we format the output?

And how are dates stored?

Again, I am sure, all will be revealed in good time.

Kate wants to know who this Ruby is that I am spending half an hour with everyday.

Sunday, June 25, 2006

The Laird (Lord in Scottish) of Kincavel

Kate, my Scottish wife, has bought me a plot of land in the Kincavel Estate. This entitles me to use the title of Laird (Owner of Land or Lord in Scottish) officially.

The Kincavel estate lies on the Ardnamurchan peninsular, which covers 50 square miles and is due west of Fort William.. There are around 2000 human inhabitants of the peninsular, but many more thousands of creatures and plants, some of which are very rare. The north coast of the peninsular faces the Islands of Muck, Eigg and Rhum, while the south coast runs alongside Loch Sunart and the Sound of Mull.

The Kincavel estate is within a large area of land belonging to the Sonachan Hotel – 1000 acres in total - 400 of which have been enclosed by fences to encourage the natural regeneration of the flora and fauna without damage by farm stock. Sonachan is located between the beautiful sandy beaches of Ardnamurchan’s north coast and Tobermory – the scene for children’s television show on BBC ‘Balamory’, which is a short ferry ride away on the Isle of Mull.

The Kincavel Estate itself is just a few hundred yards from a secluded golden sandy beach – visitors at some times of the year are likely to find they have it all to themselves! Your land is on a seaward facing hillock and looks out over the sea to the Isle of Mull. On a nice day it is the perfect place for a picnic as there are spectacular views in every direction; across the mountainous countryside inland and over the sea to the Isle of Mull.

Nearby attractions include the Ardnamurchan Natural History Centre and Ardnamurchan Point lighthouse. This lighthouse was one of many built by the Stevenson family, and is the only one in the world built in an Egyptian style. Ardnamurchan Point is the most westerly point in mainland Britain – a claim often wrongly thought to belong to Land’s End in Cornwall.

The nearest village to the Estate is Portuaik – the most westerly settlement on the mainland, overlooking Sanna Bay. From the village there are also views across to the Isle of Skye and the Small Isles.

Well, so now I am a Scottish Laird (Lord) :-).

Interesting stuff.

Saturday, June 24, 2006

Hello World in Ruby

puts 'Hello World'

And thats it. The interpreter comes to life and prints the famous words on the console.

What is interesting is after I installed Ruby, all I have to do is go to command-line and type the name of the program - Hello.rb in this case - and the program runs.

Not really impressed by SciTe as yet....but already like it better than Eclipse.

But I have so many questions...how do I write comments?...how secure is Ruby?....how do I create web-applications talking to ruby?....and I am just hoping I don't have to write the same amount of code as C or Java.

Another thing I am noticing is there are quite a few Japanese Names all over Ruby. Obviously, as Ruby was created by a Japanese person, this is to be expected. It is sad that we don't see the same amount of Solidarity within Indians. To some extent, this is our undoing.

The good thing is if it was created by a Japenese, then it will have a bigger acceptance in Japan.

Early days....lets see where we go from here.

Starting Ruby

Started with Ruby today...with installation.

I am using a Windows XP based Laptop. So used the One-click installation from
http://rubyinstaller.rubyforge.org

It is an interesting installer. Unlike the Sun JDK which requires you to download a separate IDE, the Ruby installer comes with SciTe IDE.

Lets just hope it is not a shite IDE :-).