Ruby on rails can be downloaded from many sources over the internet like from http://api.rubyonrails.org/ where there are also instructions on how to quickly install and start programming. The first lessons would be the forever basic “Hello World” program which is very easy if you get set-up right. For a more automated installation you could also check-out Instant Rails or Locomotive for users on the Mac platform.
An example of the shortest way to get from installation to your first application would be;
alias rails_hello_world=’rails hello && cd hello && ./script/generate controller welcome hello && echo “Hello World” > app/views/welcome/hello.rhtml && ./script/server -d && firefox 0.0.0.0:3000/welcome/hello’. The code was compliments of ReinH. In the following posts we would start with the declarations and other basic stuff that will get us on the right track to making more complicated procedures.
Once you have successfully installed and tried that the install was a success through the Hello World program. You now begin building projects with the following syntax : “rails ProjectName”. You then start the WeBrick webserver with “cd ProjectName enter ./script/server”. The above procedures invoke the interpreter which may be required on some platforms as a must. As we can see clearly, even on the beginning parts of learning the said language, most have to be defined as objects. Don’t worry for there are tons of free apps you can get from the web through many resource pages and users like you in the forums from all over.
Before we jump into the programming itself, let us learn some of the so-called weak points that you might be able to consider and improve on ( or even share) with others out there to make the platform a better one.
First is speed, the information that I might have come upon may be a bit outdated but it still shows a significant disadvantage when it comes to speed of processing as compared to other more stable platforms. The use of green threads is also subject to criticism by many making it very difficult or unstable in certain user-case scenarios. Unicode or multi-byte string support is not yet native and the use of global variables is the biggest pitfall so far known. This trait unfortunately allows global variables to be modified from anywhere and we mean anywhere. Ruby also lacks a well-defined specification with it being referred to as an implementation of C. Most of these issues have been thought of and are to be addressed in the coming release of the newest versions of the programming language.
Blocks or simply chunks of code as in the last example may be existing variables which changes after that same variable passes thought the block, giving it a new value. Blocks may also be used to return a needed value to the method as in the last example, the resulting value of the last expression the data was exposed to was returned back to the method as the pre-set value of the yield statement till the condition which was the set maximum value of the Fibonacci Series that was set to 1000. This is why the find method which is used for array class objects work. The method find is actually defined in the enumerable module thrown into the array class mix. The Next Post, shows a sample code using the find method.
Sample Code using find :
class Array
def find
for a in 0…size
value = himself[a]
return value in yield(value)
end
return nil
end
end
[1, 3, 5, 7, 9].find {|v| v*v >30}
The code has elements passed successively to the attached block which returns true if the method sends in a corresponding element. If none match, then nil is returned which is a very good example of how to best utilize iterators. The Array class is simply doing what it was designed to which is to access the contents of an array leaving the application to do what it is supposed to do which in this case is to find entries that satisfies a set criteria.
After it goes through the block, the file is closed taking with it the results of the block’s transactional process. This is something that should be done with all programming languages, to have the code have their own built-in file handling system which ensures that a file once opened is closed upon exit to avoid destroying the contents of that file. This shows the two forms the File.open method’s behavior can be called upon, that if it is called with a block it does what it has to and then automatically closes the file. Called on without the block and it returns the file object.
The sample code above shows the number of techniques that can be used to maximize the use of blocks for transactional control where the takeAndDosomething method is considered a class method and that it can be called independent or without influence of any single File Object. We used it to function in the same manner as the File.open method without consideration of any arguments. We called the File.open method, passed onto it the transaction or condition which was defined in the *args as a parameter. Shortening it all, the “openAndDosomething” transparently passes whatever conditions or parameters it gets towards the File.open method. Once the file ios open and the “openAndDosomething” calls on the yield statement passing the opened file to the block/chunk of code.
By Avatar | September 17, 2009
There are a variety iterators that are useful with Ruby such as we’ve already seen in previous samples of code, such as n.times. Others are n.upto and n.downto for going up or down a series of integers and the step, which is more like the traditional loop statement. Their usage would be somewhat like:
3.times {print”X “}
1.upto(3) {[i] print i, ” ” }
99.downto(96) {[i] print i, ” ” }
50.step(70, 5) {[i] print i, ” ” }
Giving you the following results:
X X X 1 2 3 99 98 97 96 50 55 60 65 70
We have looked into many of the data structures that are supported by ruby along with brief examples of their use and structures. Now we turn to the different data types the language supports which is vital in maximizing the potential of Ruby. Numbers are classified into integers and floating point numbers. The handling capabilities of Ruby when it comes to integers is dependent on the amount of memory you have installed on your computer. They are also classified into two object classes stored in either the Fixnum or Bignum classes. Ruby handles conversion to and from these two types automatically and stores them in binary form in their respective classes. Integers can also be used with optional lead signs that denote octal, hex, or binary.
Integer values can also be translated into their ASCII character set equivalents with the use of a ? before the variable. The term numeric literal that is denoted by an exponent or a decimal point is converted into a Float object in correspondence to the native language’s double data type. To obtain the absolute value of an integer you use the form aNumber.abs and not abs(aNumber) as with C and C++.
For all the good reasons, Ruby on Rails is fast becoming popular with developers and Web applications deployers. Rails is a Web application structure based on a number of advance philosophies like Convention Over configuration and Don’t Repeat Yourself. On the other hand, the JRuby on Rails presents additional benefit by allowing them to influence the immovability and the consistency of the Java platform. In lieu with this, Sang Shin is offering an online course about Ruby and JRuby which will start on July 15, 2008. Those who will attend this course will not do a real-time webcasting but definitely upon completion of the course, they will acquire all the necessary knowledge they could use to be able to write realistically refined Rails application.
Rails is a jam-packed pile skeleton for mounting web applications that are supported by a database. The developing should be in accordance with the Model-View-Control pattern. Rails is capable of providing a pure-Ruby development surrounding that include the domain model covering the database, the response based on the request sent to the controller and the Ajax in the view. To go online, you just have to add a database and a web server. Practically everyone is already on Rails, which include enterprise organizations, non-profits and even those organizations that are just starting are also inclined to using Rails. Since Rails is all about transportation and communication, it is perfectly compatible to any type of web application.