Archive for March, 2008

Hashes (Part 1)

March 29th, 2008 | 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
}

No comments

Ruby Arrays. Hashes and Ranges (Part 4)

March 25th, 2008 | Category: Basics, Sample Code

You now have an array named “Spaghetti_sauce”, that contains those elements. You can also assign multiple values into several elements with the following instruction:

Spaghetti_sauce = %w[ground pork, ground beef, tomato sauce, garlic, pepper, onions, salt, olive oil]

You end up with the same array we started with that has all the elements stored within it. There are a couple of easy ways to use and manipulate arrays such as for checking if it is empty, you simply use the following command:

Spaghetti Sauce.empty?

Checking for the total number of elements you do:

Spaghetti_sauce.size

or

puts Spaghetti_sauce.size

You can also delet elements from arrays with the delete nethod :

Spaghetti_sauce.delete “ground beef”

Looking for the first or last element you use the “.first” and “.last” method shown below :

Spaghetti_sauce.first
Spaghetti_sauce.last

If you have an unknown data structure and you want to check if it is indeed an array, you simply use the “.type” method. The “class” method is better than the “.type” method for it gives you a verbose warning.

puts Spaghetti_sauce.type (to check if the array is indeed an array)
puts Spaghetti_sauce.class (more verbose way of checking that returns an error message)

arrays also have the nift anility to store elements of different data types like characters, strings and numbers shown below:

sauce = [ 'h', 'and', 'r', '@', 20]

No comments

Ruby Arrays. Hashes and Ranges (Part 3)

March 21st, 2008 | Category: Basics, Sample Code

Array’s can be created ahead of time in say a initialization routine where you initialize all the array’s you might need in a group or block of code so you don’t have to worry about that later. To do so, you simply use the “.new” method (Spaghetti_Sauce = Array.new), which gives you an empty array named Spaghetti_Sauce. To assign values to the empty array, you simply assign values to it as with the similar example:

Spaghetti_sauce[0] = “ground pork”
Spaghetti_sauce[1] = “ground beef”
Spaghetti_sauce[2] = “tomato sauce”

More on Arrays in the next post.

No comments

Ruby Arrays. Hashes and Ranges (Part 2)

March 17th, 2008 | Category: Basics, Sample Code

Using that same example we will learn some of the basics of array’s and how they are used in our learning rails as a more of visual example. Each ingredient listed above in the recipe has a unique key assignment and that is very useful when you want to address one and only one specific element of the list. The first element of an array is always has the position “0″ so when you say Spaghetti_sauce[0], you get “ground pork” and so is the same with the many other elements. Assigning a key gets you that element and allows you to see the contents of that particular element.

More on Arrays in the next post.

No comments

Ruby Arrays. Hashes and Ranges (Part 1)

March 13th, 2008 | Category: Basics, Sample Code

We now move onto data structures which are the forms of data that Ruby operates on. Remember, the language is designed more for web applications so these are mainly basic stuff which would be followed by more complicated instructions. Arrays are used to store several data elements and can be compared to a container you use to store all the noodles in a restaurant. That restaurant has an online order facility that takes orders and tells the clerk which items are taken from the inventory for use in a particular pasta recipe. For regular spaghetti, it may look some thing like :

Sample Array:
Spaghetti Sauce: [ground pork, ground beef, tomato sauce, garlic, pepper, onions, salt, olive oil]

More on Arrays in the next post.

No comments

Documenting Ruby

March 09th, 2008 | Category: Basics, Set-Up

Documentation of a program is done by another program called the Parser which is the part of a program’s compiler that goes through a program line by line looking for all predefined characters allowing it to recognize calls to a pre-defined function in it’s library files (Similar to C’s LIB files where functions were pre-defined and called from) these pre-defined functions can be accessed by including the include file in the initialization phase of compilation where the parser recognizes the call, gets the definition of the function, uses it with the provided information and returns to the program for the next operation. This is more of the very fundamental workings of programming languages and how compilers understood the code which was written in a text editor. You will appreciate this later on when applications become more and more complex and debugging sends you tugging on your hair for hours on end to find the problem and fix it.

No comments

Comments (Part 3)

March 05th, 2008 | Category: Basics, Set-Up

The dos operations are still behind the scenes, they were just made friendlier say for getting the contents of a directory in dos, you used the “dir” command, in windows, and you did the same operation by simply clicking on a folder. Windows has the DIR command stored in code which is executed when a series of operations is done resulting in the folder opening. Ruby, though command line driven is a very powerful language for it takes the strengths of C improving it a lot with its own built in functions. Ruby treats everything as objects and yes even comments though they are not executed due to an exclusion flag of sorts.

No comments

Comments (Part 2)

March 01st, 2008 | Category: Basics, Set-Up

This is similar in Ruby for thousands of lines of code is very hard to describe manually so the RDOC. C is somewhat a sister language of Ruby and true to this many of its built-in functions are in C. If you have ever used C, you know the true power of the language. But C was initially more adept for mathematical operations rather than today’s visually based programming languages. This is still the case but the built in functions in the language allows shorter coding with the input only of required conditions or functions to make it work. Visual programming languages like Visual Basic and Visual FoxPro all use a GUI which can be compared to a system previously using DOS then having to install windows with its savvy GUI.

No comments

Bad Behavior has blocked 14 access attempts in the last 7 days.