Wednesday, May 6, 2009

Ruby Programming Language

Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was initially developed and designed by Yukihiro "Matz" Matsumoto. It is based on Perl, Smalltalk, Eiffel, Ada, and Lisp.

Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflective. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Python, Perl, Lisp, Dylan, and CLU.

The standard 1.8.6 (stable) implementation is written in C, as a single-pass interpreted language. There is currently no specification of the Ruby language, so the original implementation is considered to be the de facto reference. As of 2008, there are a number of complete or upcoming alternative implementations of the Ruby language, including YARV, JRuby, Rubinius, IronRuby, and MacRuby, each of which takes a different approach, with JRuby and IronRuby providing just-in-time compilation. The official 1.9 branch uses YARV, as will 2.0 (development), and will eventually supersede the slower Ruby MRI.

History
Yukihiro Matsumoto, the creator of Ruby.

Ruby was conceived on February 24, 1993 by Yukihiro Matsumoto who wished to create a new language that balanced functional programming with imperative programming.[1] According to Matsumoto he "wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language".[2]

[edit] Etymology of the name "Ruby"

The name "Ruby" was decided on during an online chat session between Matsumoto and Keiju Ishitsuka on February 24, 1993, before any code had been written for the language.[3] Initially two names were proposed: "Coral" and "Ruby", with the latter the one being chosen as the name by Matsumoto in a later email to Ishitsuka.[4] Matsumoto has later stated that a factor in choosing the name "Ruby" was because it was the birthstone of one of his colleagues.[5] Later it was recognized that pearl is the birthstone for the month of June, while ruby is the birthstone for July, implying Ruby as the successor of Perl.[2][5]

[edit] First publication

The first public release of Ruby 0.95 was announced on Japanese domestic newsgroups on December 21, 1995.[6][7] Subsequently three more versions of Ruby were released in two days.[3] The release coincided with the launch of the Japanese language ruby-list mailing list which was the first mailing list for the new language.

Already present at this stage of development were many of the features familiar in later releases of Ruby, including object oriented design, classes with inheritance, mixins, iterators, closures, exception handling, and garbage collection.[8]

[edit] Ruby 1.0

Ruby reached version 1.0 on December 25, 1996.[3]

Following the release of Ruby 1.3 in 1999 the first English language mailing list ruby-talk began,[2] which signaled a growing interest in the language outside of Japan. In September 2000, the first English language book Programming Ruby was printed, which was later freely released to the public further widening the adoption of Ruby amongst English speakers.

[edit] Ruby 1.9.1

As of January 30, 2009, the latest stable version of the reference implementation is 1.9.1.

Ruby 1.9.1 introduces many significant changes over version 1.8.6. Some examples are:

* Block local variables (variables that are local to the block in which they are declared)
* An additional lambda syntax (fun = ->(a,b) { puts a + b })
* Per-string character encodings are supported

[edit] Philosophy

Matsumoto has said that Ruby is designed for programmer productivity and fun, following the principles of good user interface design.[9] He stresses that systems design needs to emphasize human, rather than computer, needs:[10]
“ Often people, especially computer engineers, focus on the machines. They think, "By doing this, the machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine will something something something." They are focusing on machines. But in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves. ”

Ruby is said to follow the principle of least surprise (POLS), meaning that the language should behave in such a way as to minimize confusion for experienced users. Matsumoto has said his primary design goal was to make a language which he himself enjoyed using, by minimizing programmer work and possible confusion. He has said he had not applied the principle of least surprise to the design of Ruby,[10] but nevertheless the phrase has come to be closely associated with the Ruby programming language. The phrase has itself been a source of surprise, as novice users may take it to mean that Ruby's behaviors try to closely match behaviors familiar from other languages. In a May 2005 discussion on the comp.lang.ruby newsgroup, Matsumoto attempted to distance Ruby from POLS, explaining that because any design choice will be surprising to someone, he uses a personal standard in evaluating surprise. If that personal standard remains consistent there will be few surprises for those familiar with the standard.[11]

Matsumoto defined it this way in an interview[10]:
“ Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, 'I was surprised by this feature of the language, so Ruby violates the principle of least surprise.' Wait. Wait. The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. For example, I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprises me. ”

[edit] Features

* Object-oriented, "everything is an object"
* Six levels of variable scope: global, class, class instance, instance, local, and block
* Exception handling
* Iterators and closures (based on passing blocks of code)
* Native, Perl-like regular expressions at the language level
* Operator overloading
* Automatic garbage collecting
* Highly portable
* Cooperative multi-threading on all platforms using green threads
* DLL/Shared library dynamic loading on most platforms
* Introspection, reflection and metaprogramming
* Large standard library
* Supports dependency injection
* Supports object runtime alteration[12]
* Continuations and generators
* Methods can be called without parentheses, making Ruby popular for implementing Domain Specific Languages

Ruby currently lacks full support for Unicode, though it has partial support for UTF-8.

Read More..

Saturday, December 22, 2007

Get Free Training When You Sign Up For php works!

Here's a great opportunity to make the most out of your php|works 2007 attendance: for a limited time (and for a limited number of attendees), attendees who sign up for both the main conference and for the in-depth tutorials receive a free voucher for one of our popular online training classes—a $769 USD value absolutely free!

Hurry! Due to availability constraints, this offer is available only to the first 50 attendees!


Here's how this offer works:

* Fill in your registration form for php|works today—remember that, in order to qualify, you need to sign up for both the main conference and for the add-on tutorial day. You can also call us and sign up by phone.
* Write “Free training” followed by the name of the course (see below) you wish to attend on your sign up sheet
* Fax or mail in your sign-up sheet. Remember that payment is needed in order to qualify: make sure that you write down your credit card number on the form, or send in your cheque with it if you're mailing it
* Once we receive your form and process your registration, our training staff will contact you to schedule your course. Please note that you must attend the course you choose before September 30th, 2007

You can take one of three training courses as part of this offer

* PHP 5 Essentials—if you are new to PHP, or if you are moving to it from another language, this course will provide a thorough introduction to how the PHP platform works and give you a solid foundation of knowledge that will allow you to make the most of php|works.
* Professional PHP 5—if you are already familiar with PHP, taking this course will expose you to some of its more advanced concepts—like XML, object orientation, security, web services and more—and allow you to be ready for php|works's cutting-edge talks.
* Building Rich Internet Applications With PHP 5 and AJAX—designed for students who are looking at using PHP as the basis of their next Web 2.0 application, this course explores the creation of Rich Internet Applications with technologies like JSON, SOAP and the Yahoo! UI Library.

Why Are We Making This Offer?
At every conference, we get a huge number of requests that we include some sort of “crash course” in our tutorial day for those who want to freshen up their skills for the main conference. However, it's next-to-impossible to come up with a good PHP course that can take place in a single day, both because of the great number of topics to cover and because there is only so much that a person can cram in a single day.

Offering a free training course gives us a great opportunity to make the php|works experience truly worthwhile for all attendees. Our online training courses take place over three weeks, with plenty of time for covering all the topics properly and for the students to absorb and understand the materials properly; this way, you not only get a great deal, but you are sure to come to the conference prepared for everything the speakers will throw at you!

Terms and Conditions

* No substitutions—only the courses listed above are available for this offer
* This offer includes only access to the course. It cannot be combined with any other offer, and does not include any bonus products that may be delivered as part of our regular course packages
* This offer is not available to attendees who purchase a student pass

Read More..

Hints and tips using PHP

These tips are mainly from the problems I have encountered and the ways I have found to fix them. I realise that all of these tips are available in many other
places just by using a search engine but here I've collected some of the most useful in one place, I also take no credit for the solutions but hope they work
for you too.

Many of these tips involve sending headers with PHP, by their very nature it's often very difficult to tell if they are working since this information
does not appear in the web page. I personally use the 'Live HTTP header' extension for firefox which is available from
mozdev.

This allows you to view the headers sent by the browser and server in real time and is a major help in debugging header problems. If you don't use
Firefox you can instead visit Rex Swain's HTTP viewer, a very useful website for viewing header information.

Important note about the PHP header function.

The golden rule for sending headers in PHP is that you must send them before any other content, they cannot be sent after html output has
begun otherwise you will get the 'Warning: Cannot modify header information - headers already sent by...' error from PHP.

Sending xhtml with Strict XHTML 1.1 Doctypes


If you are using a Strict XHTML 1.1 Doctype then you need to make some changes to how the data is sent. In reality most servers
are set up to send all flavours HTML with a content type of text/html.


What this means that even though a browser such as Firefox should handle
a strict XHTML as an xml document meaning that a badly formed document will completely stop the page display giving a rather ugly error, it will instead
still parse the page but treat it just like text/html content so will forgive badly formed pages.

To prevent this we need to send pages with a content type of application/xhtml+xml so Firefox and other XHTML capable browsers switch to strict mode. This is
in fact a very easy thing to achieve since every page request comes with a HTTP_ACCEPT header so this tells us whether we are able to send as application/xhtml+xml
or plain old text/html.



I should point out at this time that Internet Explorer 6 will not accept a content type of application/xhtml+xml, although this may have been rectified by the time IE7
hopefully comes out in the summer.

Update - it seems according to this IE7 blog entry that IE7
will NOT accept a Content-Type of application/xhtml+xml, so the code shown here will be needed for a while longer. If you do send a Content-Type
of application/xhtml+xml to IE then you'll just get a save file dialog opening since IE doesn't know that to do with the file so asks if you want
to save it.



The php required for this is very simple indeed, we simple test for the prescence of the string "application/xhtml+xml" in the server variable HTTP_ACCEPT.

if (stristr($_SERVER['HTTP_ACCEPT'], "application/xhtml+xml"))
header("Content-Type: application/xhtml+xml; charset=utf-8");


To also send the correct content type to the W3C validator we need to test the user agent string for the value "W3C_Validator". This is
because the W3C validator does not set the $_SERVER['HTTP_ACCEPT'] string. We do this with the simple code below.

elseif (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator"))
header("Content-Type: application/xhtml+xml; charset=utf-8");


Headers to prevent page caching


I also had a number of problems with Internet Explorer caching dynamic content. I first noticed the problem with the menus for this website, as the menu is created with PHP
I add an opacity ( or filter in Internet Explorer ) so the current pages menu appears semi-transparent making it clear when navigating the menus which page you are currently visiting.

The problem was that with Internet Explorer this effect did not work due to the caching of the content, after some searching on Google this set of headers sent with PHP solved the
page caching problem in Internet Explorer. For me this was never an issue with Firefox so you might want to only send this header to Internet Explorer but it doesn't do any harm if sent to other browsers.

header("Expires: Tue, 01 Jan 1981 01:00:00 GMT");
header("Cache-Control: no-cache");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");


Custom error pages with php


On this site I use a custom 404 error page written in PHP. All you need to do to get this to work is go to the control panel of your web host and there should be an
option concerning custom error pages, just put the path to your page in the entry for 404 and save your changes.


One thing to note about doing this on IIS 6.0 or Apache is that the responsibilty of sending the correct header is handed over to your script, the
web server will NOT send the appropriate (Status: 404 Not Found) header for you. To do this with PHP simply add the following line to the top of
your 404 page.


If you do not do this then even though any human visitors will see your 404 page any search engine will get a 200 OK response by default and so it will think it has
found a valid page.

header("Status: 404 Not Found");



The same should be done if you use custom 403 pages ( forbidden ) but send the following header instead.

header("Status: 403 Forbidden");


If you want to send the equivalent header using Apache just use the following

header("HTTP/1.0 404 Not Found");


or

header("HTTP/1.0 403 Forbidden");



On Apache it's very easy to just upload a .htaccess file to the root of your server and put the path to your Error Document in there.
For details on this and how to redirect visitors using a custom 404 error page using PHP on Apache see the
Redirection using PHP and 404 error pages page.

Sessions and header("Location: ") function


I also had a bit of a nightmare with changes to a session variable not being saved. I was using it to moniter a logged in user and wanted to unset it after I logged out. The
logging out process first unsets the session and then immediately redirects the user using the header() function. However many times the session would not be unset
so I was unable to logout. My inititial code was something like this.

unset($_SESSION['login']);
header("Location: login");



The login page would then check if the user was logged in through the session and show different content based on the result. However it appeared the the session was never unset because if
you immediately use header("Location: somewhere") after unsetting a variable then PHP does not write the changes to the session database resulting in the changes not being saved.
The fix is very simple, after unsetting ( or any change to a session variable ) the session, and before the location header, you must close the session with this function.

session_write_close();


Compressing content


I only discovered how easy this is very recently. If you're using PHP 4.3 or later on IIS 6.0 then the compression module is already build in and ready to use.
First you need to add the following lines to the very start of your output.

ob_start();
ob_start('ob_gzhandler');



The first ob_start() starts output buffering while the second does the same while using the function 'ob_gzhandler' as the output handler. All this means is that after
PHP has finished compiloing the page it will sent the buffer to this function which will take care of the compression for us. At the very end of your page you need to
add the following code.

ob_end_flush();
header('Content-Length: '.ob_get_length());
ob_end_flush();



This code first outputs the buffer and then gets the buffer length which it adds as a header so the browser knows how much data is coming. The second flush is because
we need to calculte the content length and send this as a header, but we cannot send any data to the browser until all headers are sent so this allows us to get the
content length, send the header and then finally send the data to the browser. Basically were just emptying one buffer into a second buffer to allow us to send the
header.

IIS Extension mappings settings


If you are having problems with getting a custom 404 page to work with PHP it may have something to do with the extension mapping within IIS. When I tried to implement
a custom error page I kept getting the text "No input file specified" returned with nothing else when viewing a non-existant page. This turned out to be because PHP
was not checking to see if the file actually existed before trying to parse it so any page with a extension it attempted to parse. As a result it tried to parse
the page and reutned the generic error above. This was fixed by my web host checking the 'Check that file exists' checkbox in the extension mappings for PHP.


To find this setting, open up the IIS snap in, go to the properties of your website and select the 'Home directory' tab. Then click on 'Configuration' in the bottom
part of the window below where it says 'Application settings'. This opens up the 'Application configuration' window, select the entry for PHP in the list box at the bottom
and click 'Edit' to open the window shown below. This path summarised here:


Website Properties > Home directory > Configuration > Highlight PHP line > Edit > Check that file exists


Make sure the check box highlighted in red is checked and save your settings. Now when you visit a page that doesn't
exist on you server PHP will check to see if it's there and if not will trigger the 404 response correctly. If you do not have access to the server, as was my case,
if you log a support ticket they should be able to enable it for you.

Read More..

Java For Mac OS X

Apple has optimized Java on Mac OS X to look great and perform superbly, making Mac OS X the ultimate platform for developing and deploying cross-platform Java applications.
Java Screens: Java Applications, Xcode Development, and Web Applets

Java Compatible

Java has become the de-facto standard language for developing cross-platform applications. Recognizing this, Apple has made Java a core component of Mac OS X. Mac OS X includes the full version of Java 2, Standard Edition — meaning you have the Java Developer Kit (JDK) and the HotSpot virtual machine (VM) without downloading, installing or configuring anything. And because Apple has optimized Java on Mac OS X, Java applications act as first-class citizens on Mac OS X.
Quartz Extreme
Cross-platform Compatibility

Java applications take on the Aqua look and feel by default and implement Java’s graphics directly on top of Quartz, providing the best-looking Java ever. Mac OS X also makes Java applications leaner and faster — it reduces the memory footprint of Java applications by providing a version of Java HotSpot VM that implements a mechanism similar to shared libraries. Plus, to help developers get started out of the box, Mac OS X also includes an integrated development environment — Xcode.
Safari
Safari Supports Applets

On Mac OS X, Java applets work best in Safari, which takes advantage of the latest version of the standard Java Internet Plug-In. Applets load faster than previously and the plug-in supports new advanced caching features for Java classes and JAR files. Certificates used in signed applets are now stored directly in the Mac OS X Keychain, providing centralized access. What’s more, with the Java Plug-In, Safari supports websites that use LiveConnect for communication between JavaScript and Java applets, letting you work with more Java-based websites than ever before on the Mac.
Xserve rack
Protect and Serve

Java is one of the key components of a good server solution. That’s why it’s a vital part of the Mac OS X Server software for Xserve. In addition, Mac OS X Server includes all of the components necessary to host high-performance J2EE-based applications — including JBoss, Apache Tomcat and Apache Axis. As if that weren’t enough, Xserve also includes a deployment license of the full WebObjects Java application server, so that you can deploy sophisticated web applications right out of the box.
Less Memory, Faster Start

On other platforms, each Java application consumes some system memory, so you might end up using more memory than you need to when running multiple Java applications. Other languages, such as C or C++, solve this problem using what’s called shared libraries. Apple developed an innovative new technology that allows Java code to be shared across multiple applications. This reduces the amount of memory that Java applications normally use. And it fits right into Sun’s Java HotSpot VM, allowing Mac OS X to remain compatible with standard Java. In addition, Apple has given this implementation to Sun so the company can deploy it on other platforms. It’s just one example of how Apple supports standards and shares ideas to benefit all.
Universal Access
User Friendly

As with all other Mac OS X applications, accessible Java applications get all the accessibility benefits of Mac OS X, including full keyboard access, visual notifications and the innovative Zoom view, as well as Tiger’s new integrated VoiceOver screen reader
AppleScript
Scriptable Java Applications

Now Java applications are scriptable on Mac OS X, thanks to the new UI Scripting facility in AppleScript. You can automate your Java applications, selecting menu items, pushing buttons and exchanging data. It’s the perfect tool for testing and including Java applications in your workflows.

Read More..

Do Not Fear With OOP

When I first started learning how to program Java, I was left totally confused about this whole "object-oriented" thing. What books I had explained the concept poorly, and then went straight on to advanced programming tips. I felt frustrated and lost. Not being particularly math-oriented, I needed a good analogy to help me understand the nature of Java.

I have created this brief tutorial not in order to be an exhaustive Java resource, but rather to introduce readers to the concepts of object oriented programming in a way that is non-threatening. If all goes well, we'll have you all in pocket protectors before the end of the hour.

There are three different levels of this tutorial, coded by color. Green is for those readers who want the most basic introduction. It is targeted at those who are unsure what object-oriented programming is, and could use a good analogy to make things clearer. Yellow is for those who want to be able to understand object-oriented programming just enough to be able to read and follow it, but are not yet ready to learn the intricacies of coding Java. [Jump to a site with darker yellow.] And finally, the third level, red, is for you daredevils who want to be able to program in Java, but just want to ease into it slowly.

In short, the green text gives a "plain English" version of the code that would be necessary, the yellow uses that English in a way that more closely resembles the format of code, and the red is the actual code that would be necessary for the program to work. Readers of all levels are encouraged to skip between the colors to deepen their understanding. Finally, although this tutorial operates mostly through analogy, innuendo, and intrigue, those words that appear in boldface are the actual terms used by Java programmers (ooooh!), so try to remember them as you go along.

Read More..