Saturday, December 10, 2016

5415 Shayan sobhanian What is ruby 5558877

Ruby and Rails
WHAT IS RUBY?
Ruby is a cross-platform interpreted language which has many features in common with other ‘scripting’ languages such as Perl and Python. It has an ‘English language’ style syntax which looks somewhat Pascal-like at first sight. It is thoroughly object oriented, and has a good deal in common with the great-granddaddy of ‘pure’ OO languages, Smalltalk. It has been said that the lan-guages which most influenced the development of Ruby were: Perl, Smalltalk, Eiffel, Ada and Lisp. The Ruby language was created by Yukihiro Matsumoto (commonly known as ‘Matz’) and it was first released in 1995.
WHAT IS RAILS?
Currently much of the excitement surrounding Ruby can be attributed to a web development framework called Rails – popularly known as ‘Ruby On Rails’. Rails is an impressive framework but it is not the be-all and end-all of Ruby. Indeed, if you were to leap right into Rails development without first mastering Ruby, you might find that you end up creating applications that you don’t even understand (this is, in fact, all too common among Ruby On Rails novices). Understanding Ruby is a necessary prerequisite of understanding Rails.
Shayan Sobhanian
DOWNLOAD RUBY
You can download the latest version of Ruby from http://www.ruby-lang.org. Be sure to download the binaries (not merely the source code). On a PC you can install Ruby using the Ruby Installer for Windows: http://rubyinstaller.rubyforge.org/wiki/wiki.pl
Alternatively, if you are using the Ruby In Steel IDE, you can install Ruby, Rails, Ruby In Steel and all the other tools you will need using the Ruby In Steel ‘All-in-one installer’ available on the site’s Download page: http://www.sapphiresteel.com/

Friday, December 2, 2016

Order of operation - Shayan sobhanian

Back in elementary school, in a lesson about math, my teacher went through a long description of a topic
called the “order of operations.” We were told that some math operators had precedence over other ones. For
example, look at this assignment statement:
x = 5 + 4 * 9 / 6
What operations are done in what order? The teacher explained that the acronym PEMDAS would help
us to figure out the order. PEMDAS described the precedence order as
1. Parentheses
2. Exponents
3. Multiplication
4. Division
5. Addition
6. Subtraction
However, I thought that it was a terrible idea to have some implied, seemingly arbitrary ordering of
math operators. Let’s look at the assignment statement again:
x = 5 + 4 * 9 / 6
You must understand the PEMDAS ordering to figure it out. First, you would multiply 4 by 9, take the
result and divide that by 6, and then add 5, before storing the answer in x.
Because of my conviction for clarity, I feel that writing an assignment statement like this is an extremely
poor technique. You are writing in a way that forces future readers to have an understanding of PEMDAS in
order to infer what you meant by this statement.
Instead, it would be much clearer to both yourself and future readers if you were to use parentheses to
group operations. Using parentheses allows you to “force” the order of operations so that the steps happen in
whatever order you want. If you wanted to write the line in a way that reflects what would happen following
PEMDAS, it would look like this:
x = 5 + ((4 * 9) / 6)
When you have nested sets of parentheses, the only rule you need to know is that sets of parentheses
are evaluated from the innermost set to the outmost set. In this example, (4 * 9) is evaluated first, that result
is then divided by 6, and then 5 is added that result. If you wanted the operations performed in a different
order, you could use parentheses to create different groupings. For example:
x = (5 + 4) * (9 / 6)
These parentheses say that you should add 5 and 4, divide 9 by 6, and then multiply the results. Adding
parentheses as in the preceding statements makes your intent much clearer and does not rely on the reader
to understand the PEMDAS rules. I strongly encourage you to add parentheses like these to force the order of
operations.


CHAPTER 2 VARIABLES AND ASSIGNMENT STATEMENTS
27
First Python Programs
Let’s take everything we’ve learned in this chapter and write some very small, but useful Python programs.
We’ll start by writing a simple program to add up the value of all the one-dollar bills and five-dollar bills
that are in a wallet. Start by opening a new Python editor window (reminder: Control + N (Windows) or
Command + N (Mac), or File New). This is what that code could look like:
numberOfOneDollarBills = 3
numberOFiveDollarBills = 2
total = numberOfOneDollarBills + (numberOFiveDollarBills * 5)
print 'Total amount is:', total
Again, none of these lines are executing; they have just been entered into a file. In order to see any
results, we have to “run” the program that we have just written. First, save the file (Control + S (Windows) or
Command + S (Mac) or File Save).
The first time you save a new file like this, you must give it a name. All Python file names should end in
.py, so name this file something like MoneyInWallet.py.
Now that the file is saved, you are ready to run the program (reminder: press the F5 shortcut key or
select Run Run Module). If there are no errors in your program, you will see the output of your program
show up in the Shell. You should see this:
Total amount is: 13
If you had any errors, read the bottom line of the error message, identify what you typed incorrectly, fix
it, save, and run again.
Let’s build another simple program. In IDLE, open a new file (Command/Control N). This time we’ll
write a program to calculate how much money you should be paid for working at a job. For the first 40 hours,
you should be paid at an hourly rate. Any hours over 40 should be paid at time and a half times the rate:
rate = 10.00
totalHours = 45
regularHours = 40
overTimeHours = totalHours - regularHours
pay = (rate * regularHours) + ((rate * 1.5) * overTimeHours)
print 'For working', totalHours, 'hours, I should be paid', pay
When you have that working, you should see the following in the Shell window:
For working 45 hours, I should be paid 475.0
Here is one more program that involves just a little bit of algebra. Again, open a new file for this
program. You are probably familiar with the Pythagorean theorem for finding the hypotenuse of a triangle:
hypot2
= side12
+ side22
But we could not use the formula that way directly in a Python assignment statement. We cannot have a
square symbol attached to a variable, but we can simplify by taking the square root of both sides:
hypot = square root of (side12
+ side22
)
Then we can use the Python ** (raise to the power) operator to square both side lengths:
hypot = square root of ((side1 ** 2) + (side2 ** 2))


CHAPTER 2 VARIABLES AND ASSIGNMENT STATEMENTS
28
Finally, we can use the ** operator again. Raising something to the one-half (0.5) power is the
equivalent of taking a square root:
hypot = ((side1 ** 2) + (side2 ** 2)) ** 0.5
This is a legal Python statement. Now we can build the full program. Let’s try side lengths of 3 and 4, and
see what our program generates for the hypotenuse:
side1 = 3
side2 = 4
hypot = ((side1 ** 2) + (side2 ** 2)) ** 0.5
print 'Side 1 is', side1, ' Side 2 is', side2, ' Hypotenuse is:', hypot
In the Shell window, you should see this:

Side 1 is 3 Side 2 is 4 Hypotenuse is: 5.0