Archive for the 'Basics' Category
Ruby on Rails Development Tips

Image Source: www.web2.socialcomputingmagazine.com
Ruby on Rails is great for acquiring ideas prototyped super quick. These tips will slow down growth and make apps less portable, but definitely progress the speed your system
Ruby on rails coding style:
• Try to evade the over indulgence use of helpers since it adds overhead.
• You may think about using memcached to cache your model and library computation results.
• Use a custom configuration file for passwords and API keys instead of keeping them in your Subversion repository. Use YAML and mirror the style of database.yml.
• Use constants when desired. Instead of repeating strings like the address of your customer service reply email, set it once in a constant (in environment.rb or the appropriate environment file) and use that throughout your application.
• Keep time in UTC. A no brainer, and easy to do.
• Don’t loop through ActiveRecord models inside other models. Use keen loading if you need to exertion with multiple associated models. Better yet, write a custom SQL query and let the database do the work for you.
RoR and Text Editors Part-2
More on text editors that are used with RoR and we not discuss VIM or Vi Improved. Vim like Emacs is quite efficient and easy to use with RoR provided it is set-up properly. The nice about Vim is that it has the nifty way of highlighting syntax in ruby making it easy to trace and debug. It features advanced features such as having a selective command and insert mode with the first being the default mode upon startup. It is not a word processor so fonts and other word processor features are not to be expected.
TextPad
One of the quickest and easiest to use as a text editor for making RoR programs with straightforward interface and features mostly adept to windows users. Though considered a text editor it is capable of syntax highlighting, search , spell check and macro recording which makes it a choice of many developers.
ArachnoRuby
Is deployable on both windows based and Unix based systems and is considered to be the native editor of RoR. It was not too user friendly so it quickly lost favor of many developers who turned to the other specified editors for programming code.
Before You Start
Assuming you have even just a little background in programming with any language there are four basic programs and extensions that you need but are provided by the quick installers you’ve just downloaded to your hard drive. You have The Ruby programming language program files, the Rails part of the programming extension/platform, MySql or your database query handling system and the Apache which is a web server that you use to emulate your application’s execution on the web. Granting you have these set up properly and tested (instructions for which are included in the installer package). You can now start building your first application with RoR.
No commentsRegular Expressions and Blocks (Continued)
The caret “^” and the “&” operators are used for matching the beginning of a string, and also for the end of a string shown below:
matching =/[a-e]$/
The script would look for similar letters between “a” and “e” respectively including the end of the string. To search for a letter inside a string:
[A-Z] all uppercase letters
[a-z] all lowercase letters
[0-9] all digits(numbers)
To restrict the range, say to look for only the letters between “a” and “e”, you write it as [a-e] combined with the caret operator shown below:
[^A-Z] all other characters except uppercase letters
[^a-z] all other characters except lowercase letters
[^A-Za-z] no letters, whether upper or lower case
Regular Expression and Blocks
Ruby expressions would be shocking for the uninitiated or those who are shifting form other programming languages. If you have experience with Perl or Python, then you’re in luck for they won’t send you packing up and running in fear. The term “regular expression” is used to have a program check if it looks like something else in terms of similar characters or spacing, length or a myriad of other things that you may think of. The table form Ruby.org summarizes all the Ruby expressions and elements. Regular expressions are used for matching certain patterns such that if you’d want to check for a digit you use the \d expression or to match a space character, you use the \s expression to match a space character.
No commentsUnless
Unless is another control structure similar to “if..else’ that is for conditional processing and it is used as follows:
unless condition
instruction1
else
instruction2
end
Again, it uses the “end” statement to terminate the control structure. The “case”, control is more of a screening process that allows say the element of an array or hash to be evaluated to meet a specific set of conditions. It is used as follows:
case variable
when condition1
instruction1
when condition2
instruction2
……
……
when conditionX
instructionX
else instructionA
end
Sample “if…elseif….else”
if condition1 (is true)
instruction a
instruction b
.
.
instructionX
elseif condition2 (if the condtition 1 returns false evaluate with condition2)
instruction c
instruction d
.
.
instructionX
else (if the result is false for both levels then the following code is executed)
instruction e
instruction f
.
.
instructionX
end
The control structure evaluates the necessary ‘contition1′, if it returns true if executes instructions that follow the if statement. If it is false, it proceeds to the elseif using ‘condition2′ to evaluate it, if it satisfies condition2, then it executes code that immediately follows the elseif control. If both evaluations return false, it then proceeds to execute the instructions after the else control.
No commentsControl structures
Control structures are blocks of code that result in either a true or false and are more commonly called loops. Loops are dependent on the satisfaction of certain conditions like in the sample program below that uses the “if..else control(loop):
if condition1 (if condition 1 is true)
instruction 1
instruction 2
.
.
instructionX
else (if condition 1 is false)
instruction 3
instruction 4
.
.
instructionX
end
As you can see, the simplicity of Ruby has it working without the need for the curly braces as with C++. Ruby simply uses the “end” keyword to signify the end of the “if..else” control structure. Another way of doing the same thing but with more conditional procesing is with the the “if..elseif..else” control structure. Syntax is shown in the following post.
No commentsFunctions and Built-in Functions
A function is a way of shortening repetitive parts or blocks of code that are called again and again. Their use benefits the user by first of course, shortening the amount of code lines for having the same block of code appearing again and again wastes time and effort. It also allows the same block of code to be accessed by more than one program. It allows the programmer to break up complex tasks into smaller applications that are easily understood, and last, it allows the programmer to create and hide critical functions such as password and username encryption and decryption routines. Functions are mostly user defined but there are a couple of dozen or so that have been pre-defined and are included in the standard Ruby distribution package. Being open sourced, there are a whole range of functions that have been created and shared by the many ruby programmers out there that is shared across the whole globe.
No commentsRanges
Ranges can be better defined as a numeric array. Ranges can be defined as such:
numrange = 1..20 (numrange holds all the numbers between 1 and 20, effectively turning it into an array)
numrange2 = 1…20 (is another way of defining ranges, holding all numbers between 1 and 20 except the last number 20)
As with previous arrays and hashes, several operations can be done on them like:
puts numrange.min (gives you the lowest number in the specified range)
puts numrange.maxn (gives you the highest number in the specified range)
To check if a specified number is part of the range
numrange.include? (14)
To show the contents of the specified range:
puts numrange.to_a
No comments







