Archive for the 'Sample Code' Category

Yield statement Continued

September 10th, 2010 | Category: Basics,Sample Code

The previous post using the yield statement gives us the following output which shows the ease of using blocks in programs which can be used to pass on parameters to the other parts of the same statement.

The code from the previous post gives the output:
Hi There
Hi There
Hi There

The code between the curly braces is associated to the method three times and within that yield command is called three times in succession, each time calling the code contained within the block giving the three output code in the form of the greeting. We will discuss the concepts behind the ‘yield’ statement in the next posts as we continue to build-up up our skills while aiming to use more of the simplified methods used in ruby to further shorten the code making it easier to implement and use..

Comments are off for this post

Using the ‘yield’ statement

August 10th, 2010 | Category: Basics,Sample Code

It might be almost similar but relatively different in a big way for blocks may appear only in the source adjacent to a method call which means it should be written on the same line as the method’s last parameter and it is not implemented once it is encountered but, Ruby rather remembers the context by which the block of code appears then enters the method. Within the method itself, the block of code may be called as if it were a block in itself by using the ‘yield’ statement. After the block of code has been executed, control returns immediately right after the call to the yield statement. Sample use of ‘yield’:

def threeTimes
yield
yield
yield
end
threeTimes {puts “Hi There”}

Comments are off for this post

Simplifying the previous program with modifiers

July 10th, 2010 | Category: Basics,Sample Code

As said in the past post, there is an easier way of doing the stuff we did in the last program which would be very helpful when coding thousands of line of code when you do end up building your own programs is ruby.

class BookList
def [](key)
if key.kind_of?(integer)
result = @Books[key]
else
result = @Books.find { |aBooks| key == sBooks.name}
end
return result
end
end

Simplifying the code further by using the ‘if’ statement as a modifier it becomes a shorter easier to attain the same results as with the first program:

class Booklist
def [](key)
return @Books[key] if key.kind_of?(Integer)
return @Books.find { |aBooks| aBooks.name == key }
end
end

The use of the ‘find’ command in Ruby is simply a call to a function that is executed and it can be compared to a block call in many other languages such as Perl, C++ or Java.

Comments are off for this post

Getting Started and how to open Programs : Part 2

May 10th, 2010 | Category: Basics,Sample Code

Save the text file with the following filename “first1.rb” and we now go into the syntax checking function and the facility provided to show the syntax check verbosely. The following code shows how code is checked for syntax correctness and returns the result of the said checking by typing the following code :

$ ruby :cw first1.rb

The “-c” flag initiates Ruby’s syntax checker while the “w” flag shows the result of the syntax checker. If there is no issue with the code being checked you get a “Syntax OK” result which means you can execute the code for there are no syntax issues. Type $ruby first1.rb on the command line and you get the result ” The concatenated line is : This is my first Ruby Program ”

Comments are off for this post

Getting Started and how to open Programs : Part 1

April 10th, 2010 | Category: Basics,Sample Code

RubyonRails is similar to most programming languages where in one can use the command line in windows or a terminal in Unix based systems. All programs should be written and saves in plain text format for the compiler and interpreter to process it easily. The first program would give you a feel for the overall syntax of how the program is created and what the compiler does. Type the following code into the text file to see how it is done:

#this is a sample program
a = “This is”
b = “my first Ruby Program”
print “The concatenated line is :”
print a << b

The next post would show the continuation of the exercise.

Comments are off for this post

Next step : Creating the database for the filename application

March 10th, 2010 | Category: Basics,Sample Code,Set-Up

The next phase or step would be to create a database for the application to use. Make sure the MySql engine is running and in the command window type “mysql -u root -p” and press enter and another enter for the password when prompted for there has not been any defined password yet. You are now logged into the engine as the root user and proceed to create the database by entering the following command “create database filename_development”. Also type in “grant all on filename_development.* to ‘ODBC’@'localhost’ this tells windows to grant access to a user named ODBC so you avoid an error when you try to access the said database from the command prompt. We next tackle the creation of tables that would allow the database to store the information we send it.

Comments are off for this post

Hashes (Part 2)

December 01st, 2009 | Category: Basics,Sample Code

Top find out what each key in the hash is associated with :

salad['green salad'] => “lettuce and sweet basil”

As with arrays, there are a lot of useful methods in creating them.

salad.empty? (to check if the hash is empty)
salad.size (to check for the number of elements in the hash)
salad.keys (to get all the keys in the hash to create an array)
salad.values (to get the keys in the form of an array but not necessarily in the order they’ve been entered in the hash)

All the above operations are built-in methods that are used for such purposes.

Comments are off for this post

Hashes (Part 1)

November 29th, 2009 | Category: Basics,Sample Code

Hashes are simply arrays that link one object to another object (think of it as an association of sorts), say like Spagthetti_sauce which is part of the complete menu, referring to the hash Spaghetti_sauce gives you the ingredients. Same as with arrays, they are created with the “.new” method resulting in an empty hash.

salad = Hash.new

Using curly braces, you get to assign the hash its elements like the example shown below:

salad = {
green salad => ‘lettuce and sweet basil’
coleslaw => ‘shredded lettuce, garlic and onions, carrots’
garden fresh => ‘cherry tomatoes, lettuce, iceberg, iceberg lettuce, mayonnaise, olive oil, lemons
}

Comments are off for this post

Regular Expressions and Blocks (Continued)

October 29th, 2009 | Category: Basics,Sample Code

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

Comments are off for this post

Ruby on Rails Development Tips

August 17th, 2008 | Category: Advanced,Basics,Information,Medium,Sample Code,Set-Up


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.

Comments are off for this post

Next Page »