MDBitz - Matthew Denton
Ruby
I am always curious in learning new programming languages and have been curious for awhile now with Ruby. I have come across various scripts and applications built upon it but have never had the professional need to utilize it. Time I guess to bite the bullet and see what if anything is special about this language built on the old but probably ever living C language.
Installing Ruby
The first thing to do, with any language is installation. What to do I guess first off since I use a Windows machine is to see if thee are any executable Ruby installers. Bingo, hello RubyInstaller a self contained ruby language installer with an inclusive execution environment built in.
Creating my first Ruby script
The common file extension to a ruby script file is .rb. So starting things out I created a test script to echo the basic “Hello World” script to verify Ruby was installed correctly.
A couple things right off the back Ruby does not use semi-colons (;) to terminate a code statement, instead it uses the carriage return. Secondly the command to output to the console is puts… A strange word to use for outputting to the system it is simpler then the Java equivalent of System.out to be sure but the PHP use of echo seems more intuitive then puts.
Being familiar with the command prompt I decided to fire it up and run my script using the new installed ruby library. Having selected to add the library to my environments path from the installer there is no need to specify the path to the Ruby library.
I was pleasently delighted to have my script output correctly.
C:\test>ruby test.rb
Hello World!
Splitting a ruby command onto multiple lines
Now knowing that Ruby utilizes the new line character to specify the end of a command, I was curious as to how to wraps my ruby commands onto multiple lines. As an avid developer I try to follow common guidelines when writing code one of which is keeping each line of code to within 80 characters improving readability. A quick search revealed that the front slash (\) character can be used to “join” two code lines together.
For example:
will output:
Lots more to learn and explore next I’m going to tackle Classes and Methods in Ruby with the hope of gathering some insight into why some developers prefer Ruby as their language of choice.
Multiple Lines, My Self-Inflicted crash course introduction into Ruby, puts, Ruby, Ruby Installation, statement, Tutorial
Until the release of WordPress 3.0 due out later in the year, creating a sub navigation is still a somewhat obscure and tricky task. To hopefully shed some light on how to create a Sub Navigation for your own needs I am going to walk-through how you can output a pages child or sub pages. In simple terms how to display a second level navigation in your sidebar.
How to get Sub Pages
In WordPress the easiest way to get the a page’s child pages is to use the get_pages function. This function allows you to specify that you want the children of a page by using the child_of and parent parameters. When called this function returns an array that you can utilize to iterate over and output the child pages.
$child_pages = get_pages('child_of='. $post->ID; . '&parent=' . $post->ID;);
The $post variable is used in WordPress templates to denote the current post or page. We utilize the ID property to specify we want returned all pages that are children of the current page.
How to output the pages in a list
To output a list of the sub pages we can iterate over the children and output the page titles and links in a list by using the get_page_link function and the ID and post_title properties of the returned Page object.
echo '<ul class="subNav">';
foreach( $child_pages as $c_page ) {
echo '<li><a href="' . get_page_link($c_page->ID) . '">' . $c_page->post_title . '</a></li>';
}
echo '</ul>';
The full example
Using the above code we will be executing the code on all website pages including posts and categories. To limit the execution to only pages we can utilize the is_page function. As we want to display the secondary navigation when we are on the parent page or child page we will need to check the current page to determine if we are on a root page or child page by using the post_parent property. Using this we can determine if we need to get the children of the current page or the children of the page’s parent. Doing so allows us to consistently output a Sub Page Navigation as can be seen here.
if( is_page() ) {
$the_ID = $post->ID;
$i_page = $post;
if( $post->post_parent ) {
$the_ID = $post->post_parent;
$i_page = get_page($the_ID);
}
$child_pages = get_pages('child_of='. $the_ID . '&parent=' . $the_ID);
if( count($child_pages) > 0 ) {
echo '<li class="widget"><h2><a href="' . get_page_link($the_ID) . '">' . $i_page->post_title . '</a></h2><ul>';
foreach( $child_pages as $c_page ) {
echo '<li><a href="' . get_page_link($c_page->ID) . '">' . $c_page->post_title . '</a></li>';
}
echo '</ul></li>';
}
}
get_page, get_pages, How To, is_page, Tutorial, Wordpress
When writing a post to your WordPress powered blog or website have you ever wished you could split it up into multiple pages? The good news is that you can WordPress comes by default with the ability to create paginated posts by use of the Page-Link tag. This tag notifies WordPress that the following content should be on different page then the proceeding content.
The tag is used by simply inserting the text <!–nextpage–> into your post. There used to be a button to make this task easier on the user but it was removed to clean up the tools bar. You may find that this tag is not working for you in your site, the reason for this is that the theme you are using failed to implement the Page-Link function into their templates to display the pages. You can correct this by calling the wp_link_pages function in your single post template.
<?php wp_link_pages(); ?>
You can fully customize the page link by the usage of various parameters including before, after, next_or_number, and other self explanatory options. For a full list of options check out the Styling Page-Links page of the WordPress Codex.
How To, Muliple Pages, Page Links, Tips & Tricks, Tutorial, Wordpress, wp_link_pages
Subversion (SVN) is one of the most used source control tools for both small and large projects. I am not going to go into the benefits of using Subversion or any source control system in general, instead lets jump right into best practices for organizing your project in Subversion and how it can benefit you, your team, and your project.
Basic Folder Structure
The basic folder structure that is most commonly used for subversion is:
- trunk: The main branch of your project were the main development takes place
- tags: The tags folder is where you keep snapshots of your project, most often a release. For example if you release Version 1.1 of your project it is good practice to tag the code at that point so that you can easily rollback to version 1.1 down the road if needed.
- branches: The branches folder is where you keep experimental code or various versions of the code that are actively used.
The trunk and tags folders are pretty much self explanatory. If you reach a major milestone or release of your project tag the code so that you can easily access that version at a future time. The folder that I find most individuals fail to utilize fully is branches.
The branches folder is meant to keep various version of your project that contain either new functionality or legacy code that are not in the main trunk due to many different reasons. For example if you are working on release 2.1 of the project but you have a team member working on functionality for 2.2 then it would be a good idea to create a branch for the 2.2 project so that any changes those developers working on the new functionality make would not be included in the 2.1 version by accident.
Another use of branches is to branch the code after a release. This way if a bug is found you can easily make the change in the branch for that release and re-release without removing managing changes that occurred in the main trunk since that release. Just remember to include the bug fix in the main trunk as well if applicable.
Subversion is a very powerful but surprisingly easy tool to utilize. If you are still duplicating files and keeping flat versions on your computers or Hard Drives then take a look at subversion it can save you a lot of time and headaches remembering the correct files that you are working on.
Best Practices, Source Control, Subversion, SVN, Tutorial
MDBitz - Matthew Denton
Java
In the previous tutorial we walked through how to send email from a Java Application. This tutorial extends off that and explains how to send email from a SMTP server that requires basic User Authentication in the form of a user name and password. This is performed by use of the Authenticator object.
Prerequisites
This tutorial builds off of the previous example and as such you will need to review that tutorial to understand the requirements necessary to utilize the JavaMail API.
How to send an email from SMTP Server requiring User Name/Password Authentication
To get started lets build off of our MailUtil class we have already created by copying the class and refactoring it to BasicAuthMailUtil. For those who don’t know what refactoring is it means to rename the class.
After we have refactored the class our first step is to create our Authenticator object. Now we could do this by creating a separate class or creating an internal class. For our purposes we will create an internal class. To create an internal class you simply declare a class inside the class declaration of another class. We would do this by placing our BasicSMPTAuthenticator class stub before the closing } of our BasicAuthMailUtil class.
public class BasicAuthMailUtil {
/* existing code */
private class BasicSMTPAuthenticator
extends javax.mail.Authenticator
{
}
}
For our Authenticator to function we need to implement the getPasswordAuthentication method which will return to the caller a PasswordAuthentication object with the desired username and password
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( AUTH_USER, AUTH_PSWD );
}
You could replace the AUTH_USER and AUTH_PSWD values directly with the user and password information for the server or define them as variables of the BasicAuthMailUtil Class.
private static String AUTH_USER = "USER NAME";
private static String AUTH_PSWD = "PASSWORD";
Now that our Authenticator is created the last step is to initialize the session with our Authenticator. Therefore we will modify:
Session sess = Session.getInstance(props);
with the following which instantiates our Authenticator then obtains the Mail Session Instance based on the properties and Authenticator.
Authenticator auth = new BasicSMTPAuthenticator();
Session sess = Session.getDefaultInstance( props, auth );
Congratulations you now have a utility class that can send emails from a SMTP Server that requires user name and password authentication.
Resources
Authenticator, eMail, JavaMail API, PasswordAuthentication, SMTP, Tutorial