Friday, August 17, 2012

I'm a little bussy

Hey, so to all of my fans (all 3 of them), who actually read my blog. I'm currently a little busy with everything so I may not update this blog as often as I should. I will try to work as hard as I can and be finished quickly. This also means I don't have time to experiment with Ruby. Now you, my readers can do some experimentation, and maybe make a blog post about it. Let me know and I'll feature it.

Wednesday, August 15, 2012

How to make a simple tax (VAT) calculator

Well, I promised a tutorial. Let's get started!
We'll be building a calculator for calculating tax. If you take a look at How to calculate VAT you will find some information on VAT and also the formula for calculating it.
Firstly, here is the code:
puts "Welcome to VAT Calculator."
puts "Let me remind you to only enter numbers. Do not type any letters, currency signs or percentage signs!"
puts "Please enter the VAT rate."
vatRate = gets.chomp
puts "Thank you, you have entered a VAT rate of " + vatRate
puts "Do you wish to add VAT to a value or subtract it?"
puts "Enter the number 0 if you wish to add VAT to a value, or 1 if you wish to subtract VAT from a value."
operation = gets.chomp
if operation == "0"
 puts "You have selected to add VAT to a value."
 puts "Please enter the value you wish to add VAT to."
 mainVal = gets.chomp puts "VAT based on your entered value is " +  String((Float(mainVal) * Float(vatRate))/100)
puts "Your entered value with VAT added is " + String(Float(mainVal) + (Float(mainVal) * Float(vatRate))/100)
else
puts "You have selected to subtract VAT from a value."
puts "Please enter the value you wish to subtract VAT from."
mainVal = gets.chomp puts "VAT based on your entered value is " + String((Float(mainVal) * Float(vatRate))/100)
puts "Your entered value with VAT subtracted is " + String((Float(mainVal) - (Float(mainVal)*Float(vatRate))/100))
end
puts "Thank you for using the VAT Calculator."
gets.chomp

Secondly, I'm sorry, but blogger seems to not allow me to enter code a way that would actually make it pleasant to look at. This is the best I could do.

Thirdly, let's explain it!

puts "Welcome to VAT Calculator." - puts just prints out a string and goes to a new line.

vatRate = gets.chomp - this creates a new variable called vatRate and it waits for user input (gets) then it removes the 'enter key' (chomp). For instance, when you enter the number 23 you have to press enter to confirm it, but if we left out 'chomp' at the end, it would save the variable as 23enter, which we do not want.

if operation == "0" - if the variable operation is 0. It is in quotes because when we enter something with 'gets' we enter a string and not a number. So 0 is a number and "0" is a string.

String((Float(mainVal) * Float(vatRate))/100) - When you surround something with String() it tries to perceive it as a string. For instance String(20) will function like "20". Float() tries to take a string (let's say "23.42") and make it a Float (float is a number with decimals). This whole line is a formula for calculating VAT.

else - this can come after an 'if' statement. For instance 'if something is true, do this, ELSE do something else'.

end - this ends the 'if statement'

gets.chomp - at the last line, if we didn't have this, the application would close as soon as the calculations were done, and we would not be able to see any results. This makes the application wait until the user presses any key or just closes the application.

I hope this has been of some help,
have fun experimenting in Ruby!

In one of the future tutorials we will add a GUI (graphic user interface), so the calculator will look something like this: VAT Calculator

Monday, August 13, 2012

New Tutorial Soon

I've been playing around with Ruby some more and I've made a great little app, that can be very useful, especially to accountants, just to save some time.
More information and tutorial coming soon!

Sunday, August 12, 2012

Ruby to .exe

I've been wondering how to get these .rb files to run on my computer without me constantly having to go to the command prompt and running them.
After a little searching on the internet I found OCRA. It is a gem that you install with the instructions you can find on their website. Then you open up the command prompt or 'Command prompt with Ruby', navigate to the folder in which you have the .rb file(s) you wish to create .exe from and enter this line:
ocra filename.rb
replace filename.rb with your own .rb file.
This is all. It will run through your application and it will create a .exe file for you. You can now run it, send it to friends, sell it, whatever you want. It even creates a nice ruby icon. 

Simple Flip-a-coin Application

As I was playing around, I made a simple, and I mean SIMPLE Flip-a-coin application.
Here is the code:

puts "You are flipping a coin"
if rand.round == 0
  coin = "heads"
else
  coin = "tails"
end
puts "It is " + coin
Let me explain it a bit.
'puts' just prints out a string (string is basically a word or a sentence). One important thing to note is that 'puts' also goes to a new line after the string has been printed out. If you want to continue printing strings in the same line, use 'print' instead of 'puts'. To go to a new line with 'print' just add the newline character '\n'.
Alright, so we're printing out 'You are flipping a coin'.
We then say "if a random number between 0 and 1 (that is what 'rand' does, it gives you a random number between 0 and 1) that is rounded to the nearest integer (that is what 'round' does) is zero, create a new variable called 'coin' and make it a string, that says 'heads'. If it is not 0 then make a new variable called coin and make it's value a string 'tails'." We then end the 'if statement' with the word 'end'. One thing to note here again is that 'rand' will give you a decimal number, such as 0.8713584669602163 and this is why we have to round it either up (if it's more than 0.5) or down (if it's less than 0.5).
We then print the line "It is " and add the coin variable which has either the value 'heads' or 'tails'.
Like I said, very simple.
Here is the output:

Saturday, August 11, 2012

Beginning Ruby

I have just started playing around in Ruby and it seems like a language that I will want to learn about more. That's why I started this blog. I will be sharing some tips, tutorials, helpful links etc. regarding Ruby and later on the Rails plug-in.
Let me make this clear: I am a complete beginner when it comes to Ruby programming.
So far I've downloaded and installed the Ruby installer from http://rubyinstaller.org, Sublime Text 2 from http://www.sublimetext.com and Gimp from http://www.gimp.org.
Ruby installer is the installer (duh) for the Ruby language. You need to install Ruby before you can run Ruby applications!
Sublime Text 2 is a free (after a while it will keep asking you to purchase the full version, but you can still use it) light-weight text editor. It supports a lot of programming languages, including Ruby and it also supports plugins and Zen coding, which is very important to some people. Zen coding is just a faster way of coding. For more information on Zen coding, check out http://code.google.com/p/zen-coding.
Gimp is a free, opensource image editor. Also check out the plug-in Gimpshop (http://gimpshop.com) which makes Gimp look and feel like Photoshop.
This is all for now.
Have fun!