Simplifying the previous program with modifiers

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.

Posted in Basics, Sample Code | Tagged , , | Comments closed

Using thw [] for conditional processing

The process shown below is and example of using the [] to have the program execute a condition that searches the contents of an array till a match is found. It goes from the top to the bottom to check each and every member of the array to look for a match doing the requested process if it is found and returning nothing if nothing is found.

class BookList
def [](key)
if key.kind_of?(integer)
return @Books[key]
else
for C in 0…@Books.length
return @Books[i] if key == @Book[i].name
end
end
return nil
end
end

Though the above shown method is quite detailed and goes through the array thoroughly, there is a n easier method which we will show with the next post making the whole process not only easier but process faster.

Posted in Basics, Sample Code | Tagged , | Comments closed

Rails on Model-View-Controller

model_view_controller.jpg

Railes uses the Model-View-Controller or MVC architecture like many current web frameworks for organizing application programming. MVC in general is an architectural pattern favorably used in software engineering where the application becomes easier and less complex to modify and adjust either the visual appearance of the application or the fundamental business rules without affecting the other. Complicated web applications are indeed more difficult to design than conventional applications but with the help of MVC, it becomes a potential answer to these complexities. In MVC, the View matches up to elements of the user interface such as text, checkbox items and so on. While the Model characterizes the data of the application and other; the View corresponds to elements of the user interface such as text, checkbox items, etc. And it’s up to the Controller to direct details concerning the communication to the model of user actions like the keystrokes and mouse movements.

Posted in Information | Tagged , , | Comments closed

Getting Started and how to open Programs : Part 2

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 “

Posted in Basics, Sample Code | Tagged , , | Comments closed

Getting Started and how to open Programs : Part 1

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.

Posted in Basics, Sample Code | Tagged , , | Comments closed

Database manipulation

The database has been initialized, so now we get down to business of defining the fields in that database and include a field that we would call a foreign key which allows the establishment of the one to many relationship between the tables. This part of the process requires quite a bit of background of database creation, manipulation and handling. It also requires knowledge regarding the workings of data types. So assuming you do not have much knowledge in such areas do some more reading to give you a better understanding of what how and why they are there. The ROR developers might say that it is very easy to do and use ROR but without the background on logic formulation, data types and manipulation as well as database handling the rest of the posts which would tackle ever hardening topics and operations would be very difficult to make sense of.

Don’t get me wrong, we just want to make life easier for you and not have you not understanding anything at all.

Posted in Advanced, Basics, Medium, Set-Up | Tagged , | Comments closed

Next step : Creating the database for the filename application

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.

Posted in Basics, Sample Code, Set-Up | Tagged , , , | Comments closed

Ruby on Rails: Basic

ruby_on_rails.jpgby: Djai Tanji

Ruby on Rails (Rails or RoR) is written in the Ruby programming language which is a dynamic, reflective, general purpose object-oriented programming language that merge syntax influenced by Perl with features like Smalltalk. It is designed to make web development faster, simpler and more efficient. It supports multiple programming paradigms like object oriented, functional and imperative. It is comparable in varying respects to Python, Perl, Dylan, Lisp and CLU because of its dynamic system and automatic memory management features. Ruby is a single-pass interpreted language in its current official implementation written in C and because there is no specification for the Ruby language, the implementation is measured to be a de facto reference.

Posted in Information | Tagged , | Comments closed

RoR Application Directories in-depth

There are quite a number of directories locate in the apps folder we have just created so to de-mystify them here are some explanations which hopefully would help you out. The directory “app/controllers” is where Rails searches for the controller classes which controls the web request from the user. The next one is the, “app/views” folder which houses the templates to which the data from the app is converted to HTML and then returned to the user’s browser. The next folder houses the “apps/models” subdirectory which contains the data classes that wraps the data stored within the application’s database (this is the messiest part of the framework in other frameworks). Then last of the vital sub-directories is the “app/helpers” which hold the helper classes of data that assists the view, model and controller classes keeping them small, organized and focused on the task it is supposed to do in the first place.

Posted in Basics, Set-Up | Tagged , | Comments closed

Ruby On Rails Flexibility

As far as programming is concerned, compatibility and ease of use of the logic is what any programmer would certainly dream off. With the fast paced technology we live on today, more programming languages have made interface for people to understand how to get new means of programming logic to go by.

Rails works with a wide range of web servers and databases. For the web server, recommended are Apache, lighttpd, or nginx proxying to Mongrel (or using FastCGI). For database, use MySQL, PostgreSQL, SQLite, Oracle, SQL Server, DB2, or any of the other many systems supported can be considered as well.

Posted in Basics, Programming | Comments closed