Hello World

The (X)HTML uses tags to describe the area of the page, e.g. title tag means the title of the page. The below code displays “Hello World” in the page and the title of the page is “Hello World Tutorial”.

Cut the code from here

<html>
<title>Hello World Tutorial</title>
<body>
Hello World
</body>
</html>

if you save that as helloworld.html, and then open up the saved page with your browser of choice.

Grab data from tables

Alter the variable @tableName to the table and also the @tableWhere for the where condition, I found that if you use the standard sql dumps that you was taking allot of other crap with you as well.

The code

DECLARE @colName VARCHAR(100), @colSql VARCHAR(500), @colSQLInsert VARCHAR(500), @TYPE INT, @auto INT, @tableName VARCHAR(50), @tableWhere VARCHAR(500);
SET @tableName = 'tablename';
SET @tableWhere = 'the where condition';
 
-- grab the table column names
DECLARE tablecol cursor FOR
SELECT name, typestat, autoval FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name = @tableName);
 
--The @auto is the auto generated fields e.g. primary key. </b>
SET @colSql = '';
SET @colSQLInsert = '';
OPEN tablecol;
fetch tablecol INTO @colName, @TYPE, @auto;
while (@@fetch_status = 0)
BEGIN
       IF (@auto IS NULL)
       BEGIN
              IF (charindex('.',@colName) > 0) SET @colName = '['+@colName+']';
              SET @colSQL = @colName + ',' +@colSQL;
              IF (@TYPE = 2)
                     SET @colSQLInsert = '''''''+isnull(' + @colName + ',0)+'''''',' + @colSQLInsert;
              ELSE
                     SET @colSQLInsert = '''+str(isnull(' + @colName + ',''''))+'',' + @colSQLInsert;
       END
       fetch NEXT FROM tablecol INTO @colName, @TYPE, @auto;
END
close tablecol;
deallocate tablecol;
-- to build the sql statement, since it stops at 255 charactes split the outputs</b>
SELECT 'select (''insert into '+@tableName+' (';
DECLARE @loopingVal INT;
SET @loopingVal =0;
while (len(@colSQL) > @loopingVal)
BEGIN
       SELECT SUBSTRING(@colSQL, @loopingVal, 255);
       SET @loopingVal = @loopingVal + 255;
END
SELECT ') values (';
 
SET @loopingVal =0;
while (len(@colSQLInsert) > @loopingVal)
BEGIN
       SELECT SUBSTRING(@colSQLInsert, @loopingVal, 255);
       SET @loopingVal = @loopingVal + 255;
END
SELECT ')'') from '+@tableName + ' ' + @tableWhere;

The output will be x lines, and if you just copy them into a single line and this will display (once executed) the insert lines.

If anyone has any better methods, please comment 🙂

Hello World!!

Well, might as well do a Hello world in all of the languages. Once you have a database installed, e.g. MySQL, start up the database and goto the query line so that you are able to *talk* to the database. Type in

SELECT 'Hello World';

This should show Hello World in the output of the database.

I shall have to do Hello World in all languages now 🙂

Inheritance

Inherited classes allow for similar code to be re-used, this saves on code space and also development/testing time. Inherited classes basically have access to the internals of the “super” inherited class.

To create a inherited class
class sibling < superclass

And the sibling class can then call any methods etc within the superclass. The code example explains in code 😉

# base "Books" class
class Books
   # this is called within the .new class call
   def initialize(name, author, year)
       @name = name;
       @author = author;
       @year = year;
   end
 
   def to_s
          # return the to_s (to_string)
          "Books - Name #{@name} - Author #{@author} - Year #{@year}"
   end          
end
 
# this class is Inherited from the "Books" class
class BookToCD < Books
    def initialize(name, author, year, cdID)
           # use the "Books" class initlialize to setup the local variables within the super/inherited class
           super(name, author, year)
           #setup the local variables
           @cdID = cdID
    end
 
    def to_s
           # get the result from the super class
           result = super.to_s
           # return the result from the super class + the local cdID variable
           return result + " - CDID #{@cdID}" 
    end
end
# create a book
book = Books.new("Programming in Ruby", "Ruby Team", 2006)
# inspect outputs the internel structure of the class
puts book.inspect
puts book.to_s
 
booktocd = BookToCD.new("CD read of programming in Ruby", "Ruby Team",2006, 1)
# the inspect shows that the new class has a different memory address
puts booktocd.inspect
puts booktocd.to_s

Once executed on the command line the result is

#<Books:0x2777070 @year=2006, @name="Programming in Ruby", @author="Ruby Team">
Books - Name Programming in Ruby - Author Ruby Team - Year 2006
#<BookToCD:0x2776f38 @year=2006, @name="CD read of programming in Ruby", @cdID=1, @author="Ruby Team">
Books - Name CD read of programming in Ruby - Author Ruby Team - Year 2006 - CDID 1

As you can see the inspect method is showing that the memory block of the book and also booktocd are not the same even though they are sharing the same internals, this is Object Orientated.

Methods, Arrays, Hash

First part is the method and the second is the array and hash variables.

The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is no need to use the return keyword, but of course if the returnable value is calculated within the middle of the method and the rest is to clean up the method then use the return keyword.

# this is a basic method
def saySomething(name)
       result = "hi there #{name}"
       return result
end
 
# but Ruby will return the last evaluated line, thus this method
# is the same as the above one.
def saySomething2(name)
       "hi there #{name}"
end
 
# the two methods that are the same in there results
puts saySomething("Ian")
puts saySomething2("Ian Porter")

Below is how Ruby uses the array structure and also the hash structure, an array is defined within the [ ] brackets and the start of the array is 0, but within a hash since there is no start to the hash keys, then there is an hash key and then the values associated with that key. The hash structure is defined within { } brackets.

# this is a array
a = ['Gentoo Linux','is','great']
 
#this is a hash, an hash is a key -> value
has = { 'Gentoo' => 'Gentoo Linux',
       'RH' => 'Red Hat Linux'}       # hash
 
puts a[0]       # print the first element of the array
puts a              # print the arary
 
puts "The Key value of RH within the hash"
puts has['RH']       # print the value assoicated with the key RH
puts "The hash in full"
puts has

Once executed from the console, the output is

hi there Ian
hi there Ian Porter

for the first method demonstrating that the result value is the same

Gentoo Linux
Gentoo Linux
is
great
The Key value of RH within the hash
Red Hat Linux
The hash in full
RHRed Hat LinuxGentooGentoo Linux

Which shows the results of the program, this shows that the two methods return in the same manor and also an array is broken into separate blocks, but a hash has a continuous memory block in nature.

Class And Modules

A Ruby class is very similar to the other programming languages class structures. They allow for code to be created within a single object that can be reused etc, the code can have its own separate block of variables/methods for that instance of the required task. It is able to be inherited and extended which is different to a module which only allows for the created module to have methods and constant variables.

module SillyModule

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.