Warning: fopen(/home/wpconfig/public_html/wp-content/cache/wp_cache_mutex.lock) [function.fopen]: failed to open stream: Permission denied in /home/wpconfig/public_html/wp-content/plugins/wp-cache/wp-cache-phase2.php on line 96

Warning: Cannot modify header information - headers already sent by (output started at /home/wpconfig/public_html/wp-content/plugins/wp-cache/wp-cache-phase2.php:96) in /home/wpconfig/public_html/wp-includes/feed-rss2.php on line 8
Wordpress Blog Services » mysql http://www.wpconfig.com WP Configuration, WP templates; everything about Wordpress Sat, 31 Jul 2010 13:45:35 +0000 en hourly 1 http://wordpress.org/?v=3.0.1 Setup Multiple site on Apache 2 http://www.wpconfig.com/2010/05/09/setup-multiple-site-on-apache-2-3/ http://www.wpconfig.com/2010/05/09/setup-multiple-site-on-apache-2-3/#comments Sun, 09 May 2010 00:15:14 +0000 frank http://www.wpconfig.com/2010/05/09/setup-multiple-site-on-apache-2-3/ After we move to VPS Server, we need to learn how to setup multiple site in one server. Our current VPS using Apache 2 web server. After we googling for a while, we got some tutorial how to setup multiple site in Apache 2. This tutorial using Linux, but mostly Unix server able to use this configuration.

Setup Multiple Site - Art Cables

Setup Multiple Site – Art Cables

Preparation

After you Apache 2 installed correctly, you can tried to open on your browser, if you default server has run. Next step is added Virtual Host on your current server. Check out for running multiple site in one single IP Address

# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/www.jauhari.net
ServerName www.jauhari.net

<Directory /www/www.jauhari.net>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/nurudin.jauhari.net
ServerName www.jauhari.net

<Directory /www/nurudin.jauhari.net>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

That’s simple method with tiny configuration, on our example, we create two virtual host, one for www.jauhari.net and the other nurudin.jauhari.net, you can save this configuration on your apache2.conf in /etc/apache2/apache2.conf put this code in the end of code, and reboot you Apache2 using this command

sudo apache2ctl restart

Test Server

If you didn’t find any error, you can check on your server in web browser each site that has created. This is only simple tutorial, you can enhanced this by separated virtual host configuration in different folder and many more. I hope we can explain it later.

Image from here

PG

Author: Jauhari

This is me Nurudin Jauhari. Just human like you, I come from Gunungkidul, small place at Yogyakarta, and Today I live at Malang I was born 30/04/1980 in Ponjong, small village in Gunungkidul, Almost my time in 1 – 15 Years stay at this Village. More

Related posts:

  1. How To: Run Multiple Domains On A Local Computer
  2. Can’t Connet To MySQL Server
  3. Permalinks

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2010/05/09/setup-multiple-site-on-apache-2-3/feed/ 0
Congratulations To The Winners Of SWF & FLV Player PRO http://www.wpconfig.com/2010/05/01/congratulations-to-the-winners-of-swf-amp-flv-player-pro/ http://www.wpconfig.com/2010/05/01/congratulations-to-the-winners-of-swf-amp-flv-player-pro/#comments Fri, 30 Apr 2010 23:56:18 +0000 frank http://www.wpconfig.com/2010/05/01/congratulations-to-the-winners-of-swf-amp-flv-player-pro/ Thanks to all of the participants of contest in which you could Take A Chance To Win Personal License Of SWF & FLV Player PRO. We ran the MySql random query and selected these commenter to give them Single User licenses of SWF & FLV Player.

  1. Patrick Thomas
  2. FishFearMe
  3. Nick Chan
  4. Sirsly
  5. mlweg3
  6. Josh
  7. Pawel
  8. Anton
  9. Charles Boudinot
  10. Allan Ogg

SWF & FLV Player PRO is a smart Flash Player, enriched with numerous options and controls for viewing and managing Flash movies and videos. One of the main benefits of SWF & FLV Player PRO is the ability to save Flash Animation from the web directly to your computer. So if you want to download and save your favorite videos from such sites as Youtube, Matacafe, Google Video or other, SWF & FLV Player by Eltima is just what you need.

SWF-FLV-Player-screenshot1

More than that, with SWF & FLV Player PRO you can make a screenshot of the favorite moment and even convert the whole movie into the series of screenshots.

SWF-FLV-Player-screenshot2

Last but not least, SWF & FLV Player PRO allows playback of SWF/FLV files in full-screen mode and feel yourself at cinema while being at home with your cup of coffee.

Congratulations to all the winners from SmashingApps.Com. All of the winners will be contacted. If you are not winner at this time so please be patient and keep visiting here or subscribe to our rss feed, you can follow us on twitter and do not forget to become our fan on facebook as well. because we are coming up with another new great contest earlier next week.

Thanks to Eltima Software for sponsoring this wonderful contest at Smashing Apps.

If you want to conduct any contest at Smashing Apps just contact us and send us all the details.

Brought To You By

Premier Survey
Do you want to advertise here? Click to get more info…

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2010/05/01/congratulations-to-the-winners-of-swf-amp-flv-player-pro/feed/ 0
Business Services could be made Affiliates system http://www.wpconfig.com/2010/04/30/business-services-could-be-made-affiliates-system/ http://www.wpconfig.com/2010/04/30/business-services-could-be-made-affiliates-system/#comments Fri, 30 Apr 2010 11:55:31 +0000 frank http://www.wpconfig.com/2010/04/30/business-services-could-be-made-affiliates-system/ Affiliate business was first introduced by amazon.com by giving the marketing team to advertise its products. Since that time many emerging companies that use this system. This highly effective marketing techniques to increase sales because the company’s marketing team to be thousands and even millions of times with minimal cost because it does not pay monthly marketing team. Companies only provide incentives for sales that they do as a commission.

A unique example of what I want to give is about the Hotel Affiliation. This was quite interesting to discuss because they relate to services. Manufacturing companies typically only provide this service, but if engaged in the service have a slightly different calculation. Usually given as a sales commission, if the business service can be done on a per leads. That means every time the customer utilizing the services of the marketer’s company will get a commission for its services already rendered. An affiliate marketer if you get a lot of this kind, it will produce exceptional passive income.

Suggestion for beginner Internet Marketer :

  1. Find a good and trustee Affiliate company that proven pay out their marketer by browsing forums that provide this information or googling with those keyword.
  2. Try to find pay per leads commission. Usually it pays more litter than pay per sales but it’s continued payment. Every time customer bought those services you will earn commission from it.
  3. Start now and get ready to be a great internet marketer. :)
PG

Author: Andi Eko

I’m a blogger and content Writer. My work with PHP, Delphi, MySQL, MsSQL, and I really love games and gadget.

Related posts:

  1. What is Affiliate Marketing ?
  2. Video presenting services
  3. Virus Removal Services from Fast-teks

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2010/04/30/business-services-could-be-made-affiliates-system/feed/ 0
Take A Chance To Win Personal License Of SWF & FLV Player PRO http://www.wpconfig.com/2010/04/21/take-a-chance-to-win-personal-license-of-swf-amp-flv-player-pro-2/ http://www.wpconfig.com/2010/04/21/take-a-chance-to-win-personal-license-of-swf-amp-flv-player-pro-2/#comments Tue, 20 Apr 2010 23:45:38 +0000 frank http://www.wpconfig.com/2010/04/21/take-a-chance-to-win-personal-license-of-swf-amp-flv-player-pro-2/ Eltima Software is running a contest, where winners will get 15 personal licenses of SWF & FLV Player PRO (worth $19.95 each). Well, you guys know what SWF & FLV Player PRO is. Lets say it once again for n00bs.

SWF & FLV Player PRO is a smart Flash Player, enriched with numerous options and controls for viewing and managing Flash movies and videos.

One of the main benefits of SWF & FLV Player PRO is the ability to save Flash Animation from the web directly to your computer. So if you want to download and save your favorite videos from such sites as Youtube, Matacafe, Google Video or other, SWF & FLV Player by Eltima is just what you need.

When downloading SWF animation SWF & FLV Player PRO allows you to preview all the external resources your file may be using ( it may open and play other SWF or FLV files, load images, etc).After looking into them you can either download initial SWF file, or SWF file with all its external resources, or just external file you liked.

SWF-FLV-Player-screenshot1

More than that, with SWF & FLV Player PRO you can make a screenshot of the favorite moment and even convert the whole movie into the series of screenshots.

SWF-FLV-Player-screenshot2

Last but not least, SWF & FLV Player PRO allows playback of SWF/FLV files in full-screen mode and feel yourself at cinema while being at home with your cup of coffee.

This contest will run from April 20, 2010 to April 27, 2010 and we will select the winners randomly by running MySQL query. Result can be announced any day before April 30, 2010 so keep visiting here or subscribe to our rss feed to get the latest updates. To participate in the contest and win free license of SWF & FLV Player PRO, All you have to do is just :

1. Just leave a comment to tell us why you like SWF & FLV Player.
2. Use valid email ID in the email box.
3. Not necessary, but you can subscribe to our rss feed and follow us on twitter and do not forget to become our fan on facebook as well. (To track this contest and for future updates)
4. And thats it!

So what are you waiting for now? Let’s participate in the contest and good luck!

SWF & FLV Player

Brought To You By

Premier Survey
Do you want to advertise here? Click to get more info…

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2010/04/21/take-a-chance-to-win-personal-license-of-swf-amp-flv-player-pro-2/feed/ 0
WordPress 2.9.2 Released http://www.wpconfig.com/2010/02/16/wordpress-2-9-2-released/ http://www.wpconfig.com/2010/02/16/wordpress-2-9-2-released/#comments Tue, 16 Feb 2010 10:51:40 +0000 frank http://www.wpconfig.com/2010/02/16/wordpress-2-9-2-released/ I’ve just upgrade my personal blog into WordPress 2.9.2 this morning. There are some critical issue update beside vulnerability bug fixed. I think it’s very useful when the website has many writer content like this blog for example. The critical issue is WordPress fixing Trash facility that already launch in previous version WordPress 2.9.

At the first released of Trash facility all user can see the trash even that was not their own trash post. So, it might could be deleted by other user or something else to do. Beside this trash problem there are other bug fixed that will decrease the security problems and make this WordPress CMS more powerful.

So, by upgrading into WordPress 2.9.2 we can manage our blog/website become better and better. I hope Jauhari will upgrade this blog soon.

PG

Author: Andi Eko

I’m a blogger and content Writer. My work with PHP, Delphi, MySQL, MsSQL, and I really love games and gadget.

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2010/02/16/wordpress-2-9-2-released/feed/ 0
Script Installation Service http://www.wpconfig.com/2009/10/02/script-installation-service-2/ http://www.wpconfig.com/2009/10/02/script-installation-service-2/#comments Fri, 02 Oct 2009 17:27:42 +0000 frank http://www.wpconfig.com/2009/10/02/script-installation-service-2/

Today, many web hosting accounts use ?CPanel? as an interface that is used by a webmaster for managing the files on the domain. And within this control panel, there is an icon known as ?Fantastico?. This is a service that many web hosts subscribe to and this service allows the webmasters to automatically perform script installation for new scripts without a usual mySQL database creation, file uploading, installation and configuration.

Let’s understand the whole process from a closer look.

Script installation by ?Fantastico? includes Blogs, customer support, content management systems (cms), discussion board scripts (bulletin boards), ecommerce scripts, mailing list scripts and more. Some of you may be familiar with this that they usually require a mySQL database in order to function properly. Also they have frequent updates needed to be installed. And these updates are generally used to fix bugs, security holes or are program upgrades.

In Fantastico, there are no warnings that one should manually back up the mySQL database and other files prior installing any upgrade. And, if you fail to back up everything before you upgrade, it means that you cannot restore the files when things go wrong. Sometimes, you may have an upgrade mess that it will require you to restore the site from the backups.

When you use the automatic installer, you may not have the username and password used for the mySQL database. Actually, with some scripts, one needs the database name, database username and database password in order to reinstall the script and then restoring the data from the backup. And in case, one does not have this information, they need to backup a copy of the mySQL, so that they can import the old database backup after a new install of the script again.

And, if you ever decide to leave the current host for an another web host, it will need your back up of the database and scripts along with the usernames and passwords, so that they script works without any need for a new install on the new web host.

So, you could either learn abut the script installer process and complete the installation on your own and then record the information somewhere safe or you can use the automatic script installer under the icon ?Script Library’ inside the C-Panel. It will allow you to determine what the database name, username and password should be.

Whatever you choose, but always do regular backups of the scripts, files and your databases, you will stay protected.

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2009/10/02/script-installation-service-2/feed/ 0
Case Study: 403 Error: You are not authorized to view this page http://www.wpconfig.com/2009/09/05/case-study-403-error-you-are-not-authorized-to-view-this-page/ http://www.wpconfig.com/2009/09/05/case-study-403-error-you-are-not-authorized-to-view-this-page/#comments Sat, 05 Sep 2009 02:04:47 +0000 frank http://www.wpconfig.com/2009/09/05/case-study-403-error-you-are-not-authorized-to-view-this-page/ Solution:
Hi,

Problem Description:

In many cases when web user or blog admin can receive the message that “403 Forbeiiden  error – You are not authorized to view this page”. I have search for the solutiona and provided my best here:

Possible Solutions:

If your account is hosted on a Windows server it could be a problem with the Directory Indexes. The default configuration of the server is to load index.htm, index.html, default.htm, default.html, default.asp files at highest priority.

WordPress is done in PHP so the default page is index.php. Now this is not part of the default configuration so we need to add it in directory indes and gie it a highest priority to load at first.

You can do it either from the hosting control panel web site settings derectory indexing option or you can contact web hosting service provider.

Thanks,

Shane G.

Related posts:

  1. Case Study: WordPress redirects to index.html page Hi, Problem Summary: I have uploaded both index.php and index.html…
  2. Case Study: MySQL Error 28 In wordpress blog Hi, Problem Description: I am running with wordpress 2.7.1 and…
  3. Case Study: WordPress Error my_thread_global_end(): 1 threads didn’t exit Hi, This is the error due to the MySQL problem…

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2009/09/05/case-study-403-error-you-are-not-authorized-to-view-this-page/feed/ 0
WordPress News & Notes – April 16, 2009 http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-3/ http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-3/#comments Fri, 17 Apr 2009 01:36:04 +0000 frank http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-3/ From time to time, I run across a number of very useful WordPress resources or interesting posts related to WordPress, which I share in my WordPress news and notes posts.   Here is a few that have caught my attention over the past month or so:

  • WordPress Optimization Bible -The WordPress Optimization Bible is a collection of useful tips and tutorials on how to speed up your WordPress site. If you ever experienced slow WordPress admin panel, “MySQL server has gone away” message, pages taking forever to load or you want to prepare your site for a major increase in traffic (for example Digg front page) this is the guide for you.
  • What’s in Store for WordPress Themes in 2009? – Justin Tadlock shares his thoughts on what he expects out of WordPress themes in 2009.   In my opinion, you’ll see many more premium WordPress themes, as well as a shift towards theme frameworks and child theme releases.   Click over to see what Justin is expecting!
  • 135+ WordPress Tutorials – Instant Shift has compiled another large collection, this time featuring a number of our WordPress Tutorials and our WordPress code page.
  • The A to Z of .htaccess – Alex of Nometech has published an excellent post covering a bunch of information about the .htaccess file.    Useful resource to bookmark for any webmaster!

Related posts:

  1. WordPress News & Notes – October 23, 2008
  2. WordPress News & Notes – September 5, 2008
  3. WordPress Talk – April 15, 2008
  4. WordPress News & Notes – September 25, 2008

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-3/feed/ 0
WordPress News & Notes – April 16, 2009 http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-2/ http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-2/#comments Fri, 17 Apr 2009 01:36:03 +0000 frank http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-2/ From time to time, I run across a number of very useful WordPress resources or interesting posts related to WordPress, which I share in my WordPress news and notes posts.   Here is a few that have caught my attention over the past month or so:

  • WordPress Optimization Bible -The WordPress Optimization Bible is a collection of useful tips and tutorials on how to speed up your WordPress site. If you ever experienced slow WordPress admin panel, “MySQL server has gone away” message, pages taking forever to load or you want to prepare your site for a major increase in traffic (for example Digg front page) this is the guide for you.
  • What’s in Store for WordPress Themes in 2009? – Justin Tadlock shares his thoughts on what he expects out of WordPress themes in 2009.   In my opinion, you’ll see many more premium WordPress themes, as well as a shift towards theme frameworks and child theme releases.   Click over to see what Justin is expecting!
  • 135+ WordPress Tutorials – Instant Shift has compiled another large collection, this time featuring a number of our WordPress Tutorials and our WordPress code page.
  • The A to Z of .htaccess – Alex of Nometech has published an excellent post covering a bunch of information about the .htaccess file.    Useful resource to bookmark for any webmaster!

Related posts:

  1. WordPress News & Notes – October 23, 2008
  2. WordPress News & Notes – September 5, 2008
  3. WordPress Talk – April 15, 2008
  4. WordPress News & Notes – September 25, 2008

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2009/04/17/wordpress-news-amp-notes-april-16-2009-2/feed/ 0
Getting Started With Ruby On Rails http://www.wpconfig.com/2009/03/20/getting-started-with-ruby-on-rails/ http://www.wpconfig.com/2009/03/20/getting-started-with-ruby-on-rails/#comments Fri, 20 Mar 2009 07:09:35 +0000 frank http://www.wpconfig.com/2009/03/20/getting-started-with-ruby-on-rails/ If you’re a Web developer who’s been curious about Ruby on Rails but has never gotten around to trying it out because you couldn’t find a suitable overview of its advantages, then this article is for you.

We want to bring Ruby on Rails closer to those who want to take a peek first, without going through an entire tutorial. So, this article is structured a little different from most other introductions out there; hopefully it is more useful because of this.

I assume you’re already familiar with some other form of Web development, whether PHP, Python, Perl or Java, and relational databases like MySQL. First, we’ll introduce Rails and Ruby and the basic ideas behind both. I’ll teach you just enough Ruby so that you understand the code samples. I’ll tell you how to get Ruby on Rails running on your computer, and I’ll give you an overview of the basic functionality of Rails and demonstrate how Rails’ main parts work together.

This tutorial consists of two articles: in the current, first article we get started with some basic concepts and essential components of Ruby on Rails. In the second part (it will be published next week) you will learn how to install the engine; you’ll also take a closer look at Rails’ inner workings and discover main advantages of Ruby on Rails. Please stay tuned.

After reading these parts, you should have an idea of whether Rails is for you. If you get the feeling that it is, I’ll point you to some good tutorials on the Web that you can use to learn Rails. I’ll also provide a lot of further reading recommendations so you can dig as deep into the topic as you like.

I’m taking this approach because Rails is almost 5 years old now and has become very complex. There are a lot of “Create-your-own-blog-in-5-minutes”-type tutorials out there already, and rather than adding another one, I wanted to provide this kind of rough overview to help you decide whether to take this adventure.

You may want to take a look at the following related posts:

The Idea Behind Rails

Ruby on Rails was created by David Heinemeier Hansson as a kind of byproduct of Basecamp’s development at 37signals in 2004. Basecamp was built in Ruby because Hansson found PHP and Java not powerful or flexible enough. It was quite an obscure language back then, without the large eco-system available today. To make development easier, Hansson rolled his own Web development framework, based on simple ideas that had proven successful elsewhere. Rails is founded on pragmatism and established paradigms instead of exotic new ideas. And that’s what made it so successful.

Rails is based on the Model-View-Controller pattern that splits your application into three parts:

  • The Models are your business objects describing the structure and behavior of the problem your application is trying to solve. These are usually backed by an Object-Relational-Mapping framework that persists your objects to a database in the background.
  • The Views are the templates that render data to the user and all the logic surrounding presentational aspects of your app.
  • The Controller sits at the heart of everything, processing requests from clients, initiating changes in the models and triggering the rendering of the templates.

Rails is “opinionated software.” It doesn’t want to be everything for everyone. It focuses on one way of doing things and streamlines all its parts around that way. That’s not to say there’s no possibility of doing things differently if you need to, but you’ll definitely have it easier if you do things “the Rails way.” And that way happened to be exactly the right one not only for Hansson but for a lot of other developers, too, another important reason for Rails’ success.

Programmer productivity was the main goal during Rails’ development, not performance. This has led to a lot of controversy and claims that arise over and over about how Rails can’t scale. This is Rails’ own fault to a certain degree. In its early days, it had the image of a Web development framework messiah of hope and wonder that would lead us all to the promised land were applications wrote themselves. The Rails team didn’t do enough to keep expectations more realistic, and some people became disappointed.

While it’s true that Ruby on Rails is slower than comparable stacks on PHP or Python, it certainly does scale, as hundreds of successful deployments are proving. You’ll just need to scale sooner and put some thought into it. Remember also that Ruby’s current default implementation is terribly inefficient, but alternatives are on the way. There’s nothing inherently slow about the language, though, as blazing-fast implementations of Smalltalk (a language very similar to Ruby) prove. Ruby will only get faster. As the saying goes, you don’t have a performance problem until you have a performance problem, and all this talk should not scare you yet. You haven’t even started. ;)

Now, before I introduce you to the framework, let’s get started with Ruby.

A Gem From Japan

Ruby on Rails owes not only half its name but its entire feel and flexibility to “Ruby,” that neat little language from Japan.

Ruby came out in 1995 and was developed by Yukihiro Matsumoto, or “Matz” as he’s called in the community. Version 1.0 was released in 1999 and slowly gained recognition in the west from then on.

A key point in the spread of Ruby was the release of “Programming Ruby,” also called the “Pickaxe” (a reference to its cover illustration), by the Pragmatic Programmers. “Programming Ruby” was the first comprehensive English guide to the language and API.

Ruby was designed with simple principles in mind. Matz took the most successful and powerful elements from his favorite programming languages — Perl, Smalltak and Lisp — and combined them into one language with easy syntax. One goal was to make Ruby feel “natural, not simple” and to create a language “that was more powerful than Perl, and more object-oriented than Python.” This results in Ruby’s core principle: Everything is an object.

Objects

Let’s stop here and examine this. Really, everything is an object in Ruby. True and false are objects, literals are objects, classes are objects. You can call a method on a numeric literal:

>> 5.next
=> 6

Operators in Ruby are nothing but methods:

>> 5 * 10
=> 50
>> 5.*(10)  # times-operator called as a method (dot-notation)
=> 50       # with a parameter (in parentheses)

Ruby is extremely flexible and open. Almost everything about it can be changed or manipulated at runtime:

  • You can add and remove methods and variables to and from objects.
  • You can add and remove methods and variables to and from classes.
  • You can truly manipulate any class this way, even core classes like String and Integer!

Here’s an example:

>> "hi".repeat(4)
NoMethodError: undefined method `repeat' for "hi":String
>> class String     # Open the string class and add the method
>>   def repeat(i)
>>     self * i
>>   end
>> end
=> nil
>> "hi".repeat(4)   # Call it again on a fresh String literal
=> "hihihihi"       # And there it is!

Here, I defined the method repeat on the String core class, and it was immediately available on a string literal.

And he who giveth, taketh away:

>> class String             # Open up the method again
>>   undef_method :repeat   # And remove the method
>> end
=> String
>> "hi".repeat(4)           # Try to call it
NoMethodError: undefined method `repeat' for "hi":String

I could have also done this with predefined methods. They are no more “special” than the methods we have defined.

Let’s review the definition of repeat in the above example for some more interesting tidbits. Note that we’re not saying return anywhere in the body. That is because in Ruby, methods always implicitly return the value of their last expression. You could of course always jump out of a method by using return before reaching the last statement, but you don’t have to. The expression we’re returning is self * i. Self is equal to this in Java and $this in PHP and always refers to the current object. The times-operator on a string repeats the string as often as told by the second operand/parameter, i in this case.

Loops

You rarely see manual iterations in Ruby, like for or while loops. Instead, Collections come with their own iterators that you can pass blocks to, which are executed for every element in the collection:

a = "Hey "
[1, 2, 3].each do |num|
    puts a * num
end

# Outputs:
# Hey
# Hey Hey
# Hey Hey Hey

What you see here is an array literal containing numbers. On that array, the each method is called, an iterator that takes a block and calls the block for every element in the array. The block starts with the do, followed by a list of its parameters enclosed in pipe symbols. Here we have one parameter called num that will take on the value of the array element in each iteration. Inside the block, we’re simply outputting the result of a * num. The definition of * on Strings is to repeat the string accordingly. We could have put the String inside the Block, but I wanted to demonstrate that blocks have access to their surrounding scope.

Syntax

Ruby likes to keep the syntax clean and friendly. You can see this in the above examples. Although heavily influenced by Perl, Ruby doesn’t have Perl’s excessive use of special characters. You can use semicolons to end lines, but you don’t have to (and no Ruby programmer does). You don’t need to surround method parameters with braces in unambiguous situations (although it is recommended you do so if they enhance readability), and you especially don’t need to provide empty braces around an empty parameter list. That’s what makes accessors look so much like native properties.

Blocks are framed by do and end. You should only use equivalent curly braces if your blocks don’t span several lines. The only significant use of special characters is found at variable declaration. Variables in Ruby are prefixed with special characters to indicate their scope. Variables starting with a lowercase letter are local variables. Variables starting with an uppercase letter are constants. (This means that all classes are constants, too, since classes start with uppercase letters.) Instance variables start with an @. Class variables that are shared among all instances of a class start with @@. Finally, global variables all start with a $.

You’ll often find methods ending in ? or !. These are not special characters. It is merely conventional in Ruby to use question marks for methods that query an object for a Boolean condition, like Array#empty?, or exclamation marks for methods that are destructible:

>> a = [5, 1, 9, 2, 7]   # Create an array and store it in a
=> [5, 1, 9, 2, 7]
>> a.sort                # sort merely returns a new, sorted array
=> [1, 2, 5, 7, 9]
>> a
=> [5, 1, 9, 2, 7]       # a still is in its original order
>> a.sort!               # sort! instead sorts the original array
=> [1, 2, 5, 7, 9]
>> a
=> [1, 2, 5, 7, 9]       # a was changed

Conditionals

Conditionals in Ruby are very similar to other programming languages, with two notable exceptions. First, it’s possible to put a conditional after the statement it protects to make the code more readable:

execute_dangerous_operation() if user.is_authorized?

# is equal to

if user.is_authorized?
  execute_dangerous_operation()
end

Secondly, Ruby has not only an if but an unless. This is a syntactic nicety for when you want to check for the absence of a condition in a more readable manner:

unless user.is_admin?
  user.delete
else
  raise "Can't delete admins"
end

Symbols

Sometimes you’ll see names starting with a : (colon). These are a very special feature of Ruby called symbols. Symbols can be used to index hashes or mark states in a variable like you would with an ENUM in C. They are very similar to Strings but also very different. The point about symbols is that they don’t really occupy space in memory, and the same symbol literal always resolves to the exact same symbol:

>> "a".object_id  # object_id returns Ruby's internal identifier for an object
=> 3477510
>> "a".object_id
=> 3475550        # a new object on the heap
>> :a.object_id
=> 184178
>> :a.object_id
=> 184178         # the same literal refers to the exact same Symbol object

You’ll find them very often as parameters to methods, where they indicate how a method should work,

User.find(:all)   #find all users
User.find(:first) #find the first user

or as pointers to methods and variables (see the undef_method example in the “Objects” paragraph above).

Classes and Modules

Ruby supports single inheritance only, but for added flexibility it supports a feature called Mixins. In Ruby, it’s possible to define Modules that contain Methods and constants and to include these modules in a class via the include method. This way, you can extend the functionality of a class very easily.

Many of Ruby’s core classes even use this mechanism.Array and Hash, for example, both include the Enumerable module to provide a lot of convenience methods for iterating over their contents.

Often, Modules pose certain requirements to classes that include them. The Enumerable Module, for example, requires classes to provide at least an each method and an implementation of <=>, too, if its sorting features are to be used.

Modules also serve other purposes. Most importantly, they can be used to organize code into namespaces. Because classes are constants (which means you can’t assign another class to the same name), they can be stored in modules. These modules can then be nested to form namespaces.

These paragraphs probably won’t enable you to write Ruby programs, but you should be able to understand the code samples in this article now. If you want to explore Ruby a little, try the great interactive tutorial at Try Ruby, or take a peek at one of the books listed at the end of this article. If you just want to see some more code samples, check out the Wikipedia page on Ruby.

In the second part of this tutorial we will get rolling with Ruby on Rails, install the engine, take a closer look at Rails’ inner workings and discover main advantages of Ruby on Rails. Please stay tuned.

Related Posts

You may want to take a look at the following related posts:

About the Author

Jan Varwig is a programming language enthusiast, currently writing his CS diploma thesis about server-side JavaScript, and a software developer with 10 years of experience working for 9elements and as an independent freelancer. His blog and CV can be found at jan.varwig.org.

(al)


© Jan Varwig for Smashing Magazine, 2009. |
Permalink |
9 comments |
Add to del.icio.us | Digg this | Stumble on StumbleUpon! | Tweet it! | Submit to Reddit | Forum Smashing Magazine

Post tags: , , , ,

© %FIRST theman - visit the author for more great content.

]]>
http://www.wpconfig.com/2009/03/20/getting-started-with-ruby-on-rails/feed/ 0