Add two numbers

This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together.

The gets function is able to get a string from the console input, with the string inputted this allows the string function .to_i (to_integer) conversion for the answer to add up to integer values.

The code

print "Please enter number 1 : ";
# get the input from the console, 
val1 = gets;
print "Please enter number 2 : ";
val2 = gets;
# convert the string console inputs to_i (to_integers) and add together
print "Answer : " , (val1.to_i + val2.to_i), "\n";

save that as addtwonumbers.rb.

To run ruby addtwonumbers.rb, and the output would be similar to

Please enter value 1 : 30
Please enter value 2: 23
Answer = 53

Read/Write files

This is a tutorial on how to open and write files with Ruby, of course there is always different ways to accomplish the same task within programming languages.

The objects e.g. File, have different functions associated with them. The File.new creates a new file, the IO (InputOutput) object is an allows for different Input output tasks, which in this case to read each line of the input file (countrys.txt) and assign the value to ‘line’. The puts prints out what has been read in, and the syswrite, which was created from the File.net outputs to the output file.

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

This is the code

OutFile = File.new("sqlrubycountry.txt","w");
 
IO.foreach("countrys.txt") { |line|
        puts line
        OutFile.syswrite("insert into country(place) values (\"#{line.strip}\");\n");
}
 
OutFile.close;

if you save that as rubyreadfile.rb and also create a countrys.txt file with what ever text you like, e.g.
United Kingdom
France
Germany
United States etc.

The output of the program (ruby rubyreadfile.rb) will display each line that is read from the input file and the output file will have some other text in it, for demonstrating purposes, have done some sql code.

Hello world Ruby style

To get Ruby . Ruby is a development environment that is similar to other Object Oriented (OO) languages, but it is designed to be developed in quickly with a stable environment. Ruby can be used to create GUI’s to websites very quickly.

This first source code is the very basic Hello word!.

If you save this code

puts "Hello World";

as rubyhelloworld.rb

The file extension rb just stands for Ruby. To run the code from the command line, where you have saved the file above

ruby rubyhelloworld.rb

and the output will be

Hello World

Hope that helps people.

Method – Add Two numbers

This tutorial is using the same base code as Add two numbers, but with using the function addtwo, this takes two parameters $a and $b which are both set to 0 as the default value and returns the two values added together. The answer part of the web page uses the function to add the two numbers together.

The source code

<?php
       function addtwo($a = 0, $b = 0)
       {
              return ($a + $b);
       }
       $value1 = $_POST['value1'];
       $value2 = $_POST['value2'];
?>
<html>
       <title>PHP - Add two numbers</title>
       <body>
              <form action="addtwonumbers.php" method="post">
                     <input type="text" name="value1" value="0" />
                     <input type="text" name="value2" value="0" />
                     <input type="submit" value="Calculate values"/>
              </form>
              Answer : <?php echo addtwo($value1+$value2); ?>
       </body>
</html>

save as addtwonumbers_function.php, this program will function the same as the previous tutorial apart from the inner working will call the method. The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.

Add two numbers

This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.

The web page itself is using a form tag, this form tag allows for the web page to post data back to the server, where the php will use the posts ($_POST) array ( []).

The source code is

<?php
       $value1 = $_POST['value1'];
       $value2 = $_POST['value2'];
?>
<html>
       <title>PHP - Add two numbers</title>
       <body>
              <form action="addtwonumbers.php" method="post">
                     <input type="text" name="value1" value="0" />
                     <input type="text" name="value2" value="0" />
                     <input type="submit" value="Calculate values"/>
              </form>
              Answer : <?php echo ($value1 + $value2); ?>
       </body>
</html>

if you save that as addtwonumbers.php within the php website configured directory and then open up that page via a web browser (e.g. http://localhost/addtwonumbers.php, localhost means the local pc, just like 127.0.0.1 is the local ring e.g the pc itself to talk to itself via an IP).

Just for completeness, this is the command line interface code.

<?php
 echo "Please enter value 1 : ";
 fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input
 echo "Please enter value 2 : ";
 fscanf(STDIN, "%d\n", $value2);
 echo "Answer : " .($value1 + $value2) . "\n";
?>

Read/Write Files

This is a tutorial on how to open and write files with PHP, of course there is always different ways to accomplish the same task within programming languages.

Fopen will open up a file, the secod parameter is who to open the file, r = read, w = write.

$readin = fopen("countrys.txt", "r");

The while loop will set the variable $county equal to a line from the input file, the fscanf will scan a file with the second parameters requirements, the ‘[^\n]’ means anything that is not(^) a return character (\n).

while ($county = fscanf($readin, "%[^\n]")) {

Fwrite will output to the output file handler (the $output created with fopen), with the text in the second parameters

fwrite($output, "insert into country(place) values (\"$county[0]\");\n");

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

This is the code

<?php
       $readin = fopen("countrys.txt", "r");
       $output = fopen("sqlcountrys.txt", "w");
 
       while ($county = fscanf($readin, "%[^\n]")) {
              echo $county[0]. "\n";
              fwrite($output, "insert into country(place) values (\"$county[0]\");\n");
       }
       fclose($readin);
       fclose($output);
?>

if you save that as phpreadfile.php. and also create a countrys.txt file with what ever text you like, e.g. United Kingdom

France
Germany
United States etc.

The output of the program (php phpreadfile.php) will display each line that is read from the input file and the output file will have some other text in it, for demonstrating purposes, have done some sql code.

Hello world

This is the hello world for the php tutorial section. PHP is another Object Orientation language, to get php, if you download it from PHP.NET and you are able to use with Apache or with MS Tnternet Information Services IIS.

Once you have php installed, if you save this

<?php
       echo "Hello World!";
?>

as phphelloworld.php within the correct directory for the php engine to use the file.

Once you point your browser to the file e.g.
http://localhost/phphelloworld.php

and the output will be “Hello World!”;

Hope that helps 🙂