Murray State Information Systems



RubyBy: Timothy SparksWritten for the CSC 415 - Programming Languages CourseAt Murray State UniversityDuring the Fall 2011 SemesterBrief HistoryRuby was first released in 1995, and was created by Yukihiro Matsumoto. Matsumoto wanted “a?scripting language?that was more powerful than?Perl, and more?object-oriented?than?Python. That's why I decided to design my own language" (Bruce Stewart) The child of these ideas became Ruby, a fully Object Oriented language that can also be pseudo interpret procedurally, that is very flexible and powerful. As of this writing, Ruby 1.9.2 is the latest stable version available, while 1.9.3 is the latest version released, but not yet recommended.Syntax and SemanticsEverything inside in this paper in the Courier New font is code (i.e #This is Ruby code). This is for readability sake and should be excluded if put into an IDE.Naming SchemeRuby allows the programmer to use almost anything as a variable or method name. It is a case sensitive language that has few restrictions. Names that start with a capital letter are class names while names starting with a lower case letter are variables and method names. Instance variables, a type of private variable, must begin with the @ symbol, while class variables, variables private to outside but public inside the class, use the @@ symbols. Unlike other scripting languages like PERL, Powershell, and Bash, Ruby does not make the programmer put special symbols in front of their arrays or hashes, just for being an array or a hash. The # is usually used as the comment symbol, and Ruby allows for regular expressions, much like other scripting languages. Ruby also uses the : symbol to represent something called a symbol. A symbol is a specific instance of variable or data type, very similar to how saying in the statement OmNom pasta = new OmNom(); in C++ or C# that Pasta is a specific instance of class OmNom.Data TypesRuby allows for the standard integer, real, string, etc. data types found in other languages, and allows for custom types if needed such as complex numbers by creating new classes. As well it allows for arrays and hashes that are called very similarly to how arrays are called in third level languages. Ruby allows for implicit variable declaration, which increases the writability, but reduces the readability. The only data types that must be explicitly declared are methods and classes.Expressions and Assignment StatementsVariable initialization is simple and easy, all the programmer has to do is place the desired name of the variable, then the = symbol, and finally whatever they want the variable to be assigned to (i.e. variableName = value).What is really interesting is that you can change a variables type on the fly. Say we had an integer a = 5, if we wanted to change it into a string we would just use the line a.to_s which now has a == “5”, and is no longer an integer. It’s also possible to go from a string to a float or integer by adding .to_~ on the end of the variable name where ~ is the first letter of the type (float: .to_f, integer: .to_i, etc.).The expressions that Ruby allows are the standard *, /, +, and – for the simple arithmetic operators. Additionally Ruby has string formatting expressions (i.e. ‘\t’, ‘\n’) and regular expressions (i.e. ^hello$\w). To include a variable value in a string, use the syntax: “this is a string with value #{variableName}”. Control StructuresRuby has control structures whose syntax is very similar to those in Ada. The for loop is: for (variableName) in [1,2,3] do…end.The do statement is optional when the statement is on multiple lines, but if you put it all on one line it is required. Additionally, there is another syntax that Ruby allows for the first line in a for statement: [1,2,3].each do |varName| … end. The values that it loops through don’t need to be the same type either, [1,”two”, [3,4,5.0]].each do |varName| is also a valid statement. The while loop can be written as is with other languages:while (Boolean)…endAlternatively it can be written in one line as: (expressions) while (boolean) or in a block in a typical statementbegin ... end while (boolean)Similar to the while loop, Ruby also has an until loop with largely the same syntax placement, moving around the until (boolean) statement:until (boolean)…endThe major difference between a while and until statement is that the while loops while the (boolean) statement is true, and the until loops while the (boolean) is false. The only remaining iterative control structure is the simple loop{…} statement that exits when a break is found.Of the non iterative control statements Ruby has if, unless, case, and catch statements. If statements have the syntax, allowing if (boolean) end block, else and elsif statements. Unless is used in the same format as the if statements, but checks for falsehoods instead of truths. Unless is another way of doing an if not statement. Additionally, you can rearrange the statements to fit on a single line much like the while statement. The case statement’s syntax is very simple:case (variableName)when (value) : …when (value) : …else …endon top of being able to use case statements in the traditional manner, but you can also assign variables to case statements and whatever value or arithmetic expressions follows the : symbol. Ruby allows for “&&”, “||”, “!”, “and”, “or”, and “not” statements as valid Boolean or statements. However, how these are used can vastly change the output. The precedence order for “||” is not the same as “or”, so the result of the statement ((1==2) and (3==1) || (3==3)) will not produce the same output as ((1==2) and (3==1) or (3==3)).SubprogramsLike many other languages, Ruby allows for the programmer to define their own custom classes and methods. To create a class is simple to say class (className) … end. To create a method it’s just def (methodName)(parameterNames) … end. The parameters do not need to be declared a type since all variables are defined implicitly, also all parameters are passed by reference since behind the scenes Ruby is purely Object Oriented. The methods also always return the last expression evaluated, unless explicitly stated otherwise.Data Types and EncapsulationTo initialize a new instance of a class is: variableName = className.new. Ruby also allows some class methods to be called without a variable if the name of the method is declared as def className.classMethod … end, when this is done it’s possible to just call className.classMethod in the main program to use that method. This could be handy if your class needs a setup method, such as a cubic spline.Method overriding is possible in Ruby by creating a method of the same name as the one in the super class. Additionally, it’s possible to use the original method by using the keyword “super”, which useful for appending strings or information. The programmer can also declare methods to be private, public, or protected much like how you can in other languages.Like most every language, programmers can include external packages, be they custom or standard libraries, by putting: require ‘libraryName’ at the top of the file. Ruby also has file IO capabilities by using a class called IO. Once again, the syntax is very similar to Ada. File is a subclass of IO that has methods to open, close, read and write to whichever file is being processed.Object-Oriented CapabilitiesDespite appearing to be procedural, all of ruby is done in Object-Oriented Programming (OOP). Because of this, OOP is not only doable but encouraged which comes in handy for “Ruby On Rails” or “Rails” for short, which is the plugin that allows for web development.ConcurrencyUp to version 1.8 a pseudo concurrency was used, where you could create threads and they would be passed processor time, but still only used one core, which created an illusion of concurrency while still not being so. This was thanks to the Global Interpreter Lock (GIL), which only passed one thing to the processor at a time. In version 1.9 multithreading support was added where the GIL could actually throw multiple items towards the processor at a time. JRuby, a Java implementation of Ruby also allows for multithreading since it has no GIL and instead uses a Java compiler, even when using version 1.8.Exception Handling and Event HandlingMany third level languages, such as C++, Java, and C#, have try-catch blocks to allow exception handling. Much like these, Ruby has rescue blocks:begin…rescue (Exception)…endThis rescue block attempts to execute the code after the begin, much like inside the try{} block in C#, and if an exception is thrown, the code inside the rescue command block corresponding to the exception thrown: just like the catch{} block. To add more rescue blocks for more exceptions, the programmer can just add them before the end. After all exceptions are thrown additional work or cleanup may be required, in C# there is the finally{} block; in Ruby there is the ensure block, that will execute regardless of the outcome of the rescue block. The programmer may also want to only run a section of code if the main code successfully runs (i.e. process a file after making sure that the file exists), which is where the else command comes in handy. If the code before the rescue block executes before without an exception being raised, the code inside the else block is executed. Ruby also has a keyword “retry” that tells the program to retry the code before the rescue block, which is useful if the exception or error may be user based or just a spontaneous, unique event. There is also the capability to re-raise the exception, while seeming counter intuitive; this can be very helpful if the programmer wants to send the exception to a method. Ruby also allows for programmers to create custom exceptions thanks to the OOP.Other Issues or Interesting FeaturesIn addition to the built in IO file properties, Ruby also uses YAML, a language and library that allows data to be written straight to the hard drive instead of to a file.There are a number of IDEs and compilers for Ruby, ranging from stand alone to plugins for professional environments such as Visual Studio. The official compilier is free as are several IDEs like Aptana, while others such as Ruby in Steel (Visual Studio plugin) and RubyMine all cost a little bit of money. Most of the IDEs require that the programmer separately install Ruby then provide a directory path to allow compilation. On the official Ruby site, there are links to several forms of installing, from the source code to third party software, to package management systems for Linux, Mac OS, and Windows.Ruby On Rails is a set of tools and libraries that allow Ruby code to be used to develop a web environment. It is so commonly used, some people mistake Ruby for Rails, and people talk about programming in Rails as if it was its own language. Rails allows the programmer to use the simplicity of a scripting language to access databases, create web forms, and manage events such as button clicks.Evaluation of Ruby Language ReadabilityDue to the persistent OOP it can be difficult to follow Ruby code. Because of the ability to change variable types, anyone trying to understand the code must read every line and practically compile it in their head. While not extremely terrible, it can be annoying and makes it not easy to read.WritabilityWith the inclusion of regular expressions, if the programmer can get used to them, programs can be written to process various types of data extremely easily. The OOP and implicit variable declaration also increase the ease at which code can written. What is taken away from readability is given back in writability and speed at which it can be written.ReliabiltiySince there are compilers provided for free on the official site for the three main operating systems as well in Java, a language that works on as many platforms as possible, it is highly reliable that the same code that works on one computer will work on all others.CostWhile the compiler is open source and free as are several IDEs, there are some IDEs that do cost some money. The Ruby in Steel IDE, which is a Visual Studio plugin, costs $249.00 to license, which doesn’t include the $799 - $3,799.00 for Microsoft’s Visual Studio 2010. RubyMine, another IDE, costs either $69 or $149 depending on whether the purchaser is a company/organization or an individual developer. Unless the programmer or their company is dead set on one of these environments, Ruby is a completely free language to learn and use with lots of documentation on the internet. When I downloaded the Aptana IDE, a free IDE, to study Ruby with, it even came with a free public .pdf file of a book that teaches Ruby and is a great resource.Not all valuable things sparkle like silver though, time is a precious resource that cannot be replaced, and as it is said “time is money.” If the person learning already knows a programming language decently, it may take them a week to pick up Ruby if it is troubling them. If they know a scripting language and are familiar with regular expressions it could be less. They won’t be an expert in the language in this time, but they will know enough to get most tasks completed.Overall EvaluationRuby is an effective, powerful and dynamic scripting language that is easy to pick up and use. There is a large online community with plenty of documentation and open source equipment to use. It may not be the best language to learn as a first language, but is highly recommended thereafter.BibliographyAddison Wesley Longman, Inc., Programming Ruby, October 24 2011, , October 25, 2011, Collingbourne, The Book of Ruby, 2009, ? Huw Collingbourne, associated website: Stewart, An Interview with the Creator of Ruby, November 29, 2001 , October, 24, 2011 Cooper, Ruby Inside, May 13, 2009 , JetBRAINS, October 24, 2011, Official, October 24, 2011, W. Sebesta, Concepts of Programming Languages, ? 2010 Pearson Education, Inc., Ninth Edition ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download