Method – Add Two numbers

This tutorial uses the same code as Add two numbers but includes a function/method to add two numbers together and return a value. The addtwo function has two parameters that are returned back added together. I have called them a and b, so that they are different names to the variables within the main method.

The source code

# define a function called addtwo with two parameters
def addtwo(a, b) 
       a + b; # return a + b
end
 
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 as addtwonumbers_function.rb, 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 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.

C++ DLL Objects accessed from C#

Because there is a few things that c# cannot do compared to c++, e.g. cpu/memory management. And also there is probably allot of dll’s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language.

To create an dll within Visual Studio 2005 within the language c++, if you do the following

1. New project -> c++ -> empty project
2. Enter project name (e.g. hellocdll)
3. Right click source files ( in the solution explorer) add-> new item
enter an cpp filename.
4, Right click on the main project heading in the solution explorer -> properties
configuration properties->general inner screen project defaults -> configuration type, alter to dynamic library (.dll)
5. Copy and paste code and then compile.

#include <stdio.h>
 
extern "C"
{
  __declspec(dllexport) void DisplayMessageFromDLL()
  {
              printf ("Hi From the C DLL!\n");
  }
 
  __declspec(dllexport) int DisplayValueAndReturn(int i)
  {
         printf("Value %i\n", i);
         return i+2;
  }
}

The extern “C” means that the references within the code are going to be available externally and marco __declsepc(dllexport) is for the MS compile to inform that functions are to be available within an dll file.

To create an c# project to communicate with the above dll

1. New project -> c# -> empty project
2. Enter project name (e.g. hellodlltest)
3. Right click on the project name ( in the solution explorer) add-> new item
select class and enter a filename
4. Copy and paste the code and then compile. (you may need to change the Dllimport to the dll file name that has been created from the c dll project)

using System;
using System.Runtime.InteropServices;     // DLL support
 
namespace hellodlltest
{
    class hellodllC
    {
        [DllImport("hellocdll.dll")]
        public static extern void DisplayMessageFromDLL();
 
        [DllImport("hellocdll.dll")]
        public static extern int DisplayValueAndReturn(int i);
 
        static void Main ()
           {
                  Console.WriteLine ("C# program 'talking' to an C DLL");
            DisplayMessageFromDLL();
            Console.WriteLine(DisplayValueAndReturn(3).ToString());
           Console.ReadLine()
           }
    }
}

5. Copy the dll file from the debug directory of the c++ created dll project and place into the created bin/debug directory for this c# project

Define – the marco that can save time

The define part of the c/c++ language is able to save on coding time. The define is only really good for a one line of code that a function would be far to much of a over kill.

Here are three examples of the define,

#define MAXVALUE 20

this will allow for a hard coded value to be used within the coding

#define PrintHello cout << "Hello" << endl;

this will print hello when you call the marco as

void main()
{
       PrintHello;
}

and last is passing parameters to the marco

#define PrintWord(word) cout << word << endl;

will change the word parameter within the code to the passed value for example both will work

void main()
{
       PrintWord("hi there");
       PrintWord(3);
}

since the marco will convert the passed parameter to the value.

Operators

An operator syntax is part of the standard C/C++ syntax that allows you to use the +/-/* etc aspects of the objects that are assoicated with them, for example, for two values of an integer you can add these two values together with value1 + value2, the adding part is calculated within the operator and the value is returned.

To code a operator you just need to use the operator () function. Within the code below there are 3 types of operators for the operate test class, to add another integer to the internal value, to increament the internal value and also to logically add the internal value with another integer value.

The code

#include <iostream>
 
using namespace std;
 
class optest
{
public :
       int i ;
       optest() { i = 0; }
       optest(int i2) { i = i2;}
 
       virtual int out() { return i;}
 
       int operator +(int addi)
       {
              return i + addi;
       }
 
       int operator++(int)
       {
              return ++i;
       }
 
       int operator &(int addi)
       {
              return i&addi;
       }
};
 
class optest2 : public optest       // inhert the base class for showing how to use operators within base class and inherited classes
{
public:
       int out() { return i + 10;}
       optest2() : optest() {;}
       optest2(int i) : optest(i) {;}
};
 
int main(int argc, char* argv[])
{
       // setup and demo the results of the operators
       optest op(3);
       optest2 op2(2);
       cout << op.out() << endl; 
       cout << op + 3 << endl;
       cout << op.out() << endl;
       cout << op++ << endl;
       cout << op.out() << endl;
       cout << op2.out() << endl;
       return 0;
}

I have included Microsoft Visual Studio 2005 downloadable edition (here) and also Linux code for this tutorial as a download.