Ruby 1.9 and 2.0: Learn How to Write Elegant and Productive Code with The Pragmatic Programmers' Guide
What is Ruby?
Ruby is a dynamic, open source, object-oriented programming language that was created by Yukihiro Matsumoto (also known as Matz) in the mid-1990s. Ruby is influenced by several other languages, such as Perl, Smalltalk, Lisp and Python, but has its own unique features and philosophy.
Programming Ruby 1.9 2.0 Epub Download
Ruby is designed to be expressive, elegant, concise and natural. It follows the principle of least surprise, meaning that the language should behave in a way that is consistent and intuitive for the programmer. Ruby also supports multiple paradigms, such as functional, imperative, procedural and metaprogramming.
Ruby is widely used for web development, especially with the popular Ruby on Rails framework, but also for other domains, such as data analysis, testing, scripting and game development. Ruby has a large and active community of developers who contribute to its development and maintenance, as well as to its rich ecosystem of libraries and tools.
What's new in Ruby 1.9 and 2.0?
Ruby 1.9 and 2.0 are the latest versions of Ruby that introduce significant changes and improvements compared to Ruby 1.8, which was released in 2003. Some of the major changes are:
New syntax and semantics: Ruby 1.9 and 2.0 have new syntax features, such as keyword arguments, lambda literals, hash literals with colons, splat arguments in method definitions, etc. They also have new semantics features, such as local variable scoping in blocks, fibers for lightweight concurrency, encoding support for multilingual strings, etc.
Performance enhancements: Ruby 1.9 and 2.0 have improved performance in terms of speed, memory usage and compatibility with other platforms and libraries. They use a new virtual machine called YARV (Yet Another Ruby VM) that executes bytecode instead of interpreting source code directly.
New standard libraries: Ruby 1.9 and 2.0 have added new standard libraries that provide useful functionality for various tasks and domains. Some of these libraries are: Enumerator::Lazy for lazy evaluation of potentially infinite sequences; Refinements for scoping modifications to existing classes; JSON for parsing and generating JSON data; Psych for YAML processing; etc.
How to get started with Ruby 1.9 and 2.0?
If you want to learn how to use Ruby 1.9 and 2.0 for your programming projects, you need to follow these steps:
Install Ruby: You can download the latest version of Ruby from https://www.ruby-lang.org/en/downloads/ and follow the instructions for your operating system. Alternatively, you can use a Ruby version manager, such as RVM or rbenv, to install and manage multiple versions of Ruby on your machine.
Run Ruby: You can run Ruby programs in two ways: by using the interactive Ruby shell (IRB) or by using the Ruby interpreter (ruby). IRB is a command-line tool that lets you execute Ruby code interactively and see the results immediately. You can launch IRB by typing irb in your terminal. Ruby interpreter is a command-line tool that lets you execute Ruby code from a file. You can run a Ruby file by typing ruby filename.rb in your terminal, where filename.rb is the name of your Ruby file.
Write Ruby: You can write Ruby code using any text editor or IDE of your choice. However, some editors and IDEs may have features that make writing Ruby code easier and more enjoyable, such as syntax highlighting, code completion, debugging, etc. Some of the popular editors and IDEs for Ruby are: VS Code, Atom, Sublime Text, RubyMine, etc.
To write a basic Ruby program, you need to follow the basic syntax and structure of the language. Here is an example of a simple Ruby program that prints "Hello, world!" to the standard output:
# This is a comment puts "Hello, world!" # This is a method call
The first line is a comment that starts with a hash sign (#) and is ignored by the interpreter. The second line is a method call that invokes the puts method with the argument "Hello, world!". The puts method prints its argument to the standard output followed by a newline character.
How to use Ruby 1.9 and 2.0 effectively?
To use Ruby 1.9 and 2.0 effectively, you need to understand the syntax, semantics, built-in classes, modules and methods of the language. Here is a brief overview of some of the most important aspects of Ruby 1.9 and 2.0:
Syntax
Ruby syntax is based on expressions, statements and blocks. An expression is a piece of code that evaluates to a value, such as a literal, a variable, an operator or a method call. A statement is a piece of code that performs an action, such as an assignment, a control structure or a definition. A block is a piece of code that is enclosed by curly braces () or by the keywords do and end, and that can be passed as an argument to a method or an iterator.
Ruby syntax is flexible and allows you to write code in different ways depending on your preference and style. For example, you can omit parentheses around method arguments or use alternative keywords for some control structures. However, you should follow some conventions and best practices to make your code readable and consistent.
Semantics
Ruby semantics is based on objects, messages and methods. Everything in Ruby is an object, meaning that it has a state (represented by instance variables) and a behavior (represented by methods). Objects communicate with each other by sending messages, which are method calls with arguments. Methods are defined by classes or modules, which are also objects themselves.
Ruby semantics is dynamic and reflective, meaning that it allows you to change the behavior of objects at runtime and to inspect their internal structure and state. For example, you can add or remove methods from classes or objects using metaprogramming techniques, such as define_method, remove_method, send, etc. You can also access the class hierarchy, instance variables and methods of any object using reflection methods, such as class, instance_variables, methods, etc.
Built-in classes
Ruby has many built-in classes that provide common functionality for various data types and concepts. Some of the most important built-in classes are:
Numeric: The superclass of all numeric classes, such as Integer, Float, Rational, Complex, etc. It provides methods for arithmetic operations, conversions, comparisons, etc.
Array: The class for ordered collections of objects that are indexed by integers. It provides methods for accessing, modifying, searching, sorting, etc.
Hash: The class for unordered collections of key-value pairs that are indexed by any object. It provides methods for accessing, modifying, searching, merging, etc.
Symbol: The class for immutable objects that represent names and identifiers. They are often used as keys in hashes or as method names. They are created by prefixing a string with a colon (:), such as :name or :"Hello world".
Regexp: The class for regular expressions that are used to match patterns in strings. They are created by enclosing a pattern in slashes (/), such as /\d+/ or /Ruby/i.
Range: The class for sequences of objects that have a start and an end point. They are created by using two dots (..) or three dots (...) between two objects, such as 1..10 or 'a'...'z'.
Struct: The class for creating lightweight data structures that have a fixed set of attributes. They are created by using the Struct.new method with a list of attribute names, such as Person = Struct.new(:name, :age).
Time: The class for representing dates and times. It provides methods for creating, parsing, formatting, comparing, etc.
TrueClass, FalseClass, NilClass: The classes for representing the three special values in Ruby: true, false and nil. They have only one instance each: true, false and nil.
Exception: The class for representing errors and failures that occur during the execution of a program. It provides methods for creating, raising, rescuing, etc.
Modules
Ruby modules are collections of methods and constants that can be included in other classes or modules to share functionality and avoid name clashes. Modules can also be used as namespaces to group related classes or constants under a common name.
Ruby modules are defined by using the keyword module, followed by the module name and an optional block of code. For example:
module Math PI = 3.14159 def self.sin(x) # some code to calculate sine end end
This defines a module named Math that contains a constant PI and a class method sin. To access the members of a module, you need to use the scope resolution operator (::), such as Math::PI or Math.sin(0).
To include a module in a class or another module, you need to use the keyword include, followed by the module name. For example:
class Circle include Math # include the Math module def initialize(radius) @radius = radius end def area PI * @radius 2 # use the PI constant from Math end end
This includes the Math module in the Circle class, which means that the Circle class can access the methods and constants defined in Math as if they were its own. For example:
c = Circle.new(5) puts c.area # prints 78.53975 puts c.sin(0) # prints 0.0
What are the standard libraries in Ruby 1.9 and 2.0?
In addition to the built-in classes and modules, Ruby also provides a set of standard libraries that extend the functionality of the language for various tasks and domains. These libraries are not loaded by default, but can be required by using the keyword require, followed by the library name. For example:
require 'json' # load the JSON library data = name: "Alice", age: 25 # create a hash json = data.to_json # convert the hash to a JSON string puts json # prints "name":"Alice","age":25
Ruby 1.9 and 2.0 have 97 standard libraries, covering topics such as:
Data formats: csv, json, yaml, rexml, rss, etc.
Networking and web: cgi, ipaddr, net/http, net/smtp, openuri, socket, webrick, etc.
System programming and CLI: etc, fcntl, open3, optparse, pty, readline, syslog, win32ole, etc.
Cryptography and encoding: base64, digest, openssl, etc.
Miscellaneous: benchmark, coverage, debug, fiddle, logger, pp, ripper, tracer, etc.
For a complete list and description of all the standard libraries in Ruby 1.9 and 2.0, you can refer to the official documentation at https://ruby-doc.org/stdlib-2.0.0/.
Why should you learn Ruby 1.9 and 2.0?
Ruby 1.9 and 2.0 are the most modern and advanced versions of Ruby that offer many benefits and advantages for your programming projects. Some of the reasons why you should learn Ruby 1.9 and 2.0 are:
Ruby is a powerful and expressive language that lets you write less code and do more with it. It has a clear and consistent syntax that is easy to read and write. It has many features that make programming fun and enjoyable, such as blocks, iterators, metaprogramming, etc.
Ruby is a flexible and adaptable language that supports multiple paradigms and styles of programming. You can use Ruby for object-oriented programming, functional programming, procedural programming or metaprogramming. You can also mix and match different paradigms and styles to suit your needs and preferences.
Ruby is a dynamic and reflective language that allows you to change the behavior of objects at runtime and to inspect their internal structure and state. This gives you more control and freedom over your code and enables you to create powerful abstractions and DSLs (domain-specific languages).
Ruby is a portable and compatible language that runs on many platforms and integrates with many libraries and frameworks. You can use Ruby on Windows, Linux, Mac OS X or any other operating system that supports Ruby. You can also use Ruby with C or Java libraries or frameworks using FFI (foreign function interface) or JRuby (a Ruby implementation on the Java Virtual Machine).
Ruby is a popular and growing language that has a large and active community of developers who contribute to its development and maintenance, as well as to its rich ecosystem of libraries and tools. You can find many resources and support for learning and using Ruby online or offline, such as books, tutorials, blogs, podcasts, courses, conferences, meetups, etc.
Conclusion
In this article, we have learned about Ruby 1.9 and 2.0, the latest versions of Ruby that introduce significant changes and improvements compared to Ruby 1.8. We have covered the following topics:
What is Ruby? A brief overview of Ruby's history, features and philosophy.
What's new in Ruby 1.9 and 2.0? A summary of the major changes and improvements in Ruby 1.9 and 2.0 compared to Ruby 1.8.
How to get started with Ruby 1.9 and 2.0? A step-by-step guide on how to install, run and write basic Ruby programs using Ruby 1.9 and 2.0.
How to use Ruby 1.9 and 2.0 effectively? A comprehensive reference of the syntax, semantics, built-in classes, modules and methods of Ruby 1.9 and 2.0.
What are the standard libraries in Ruby 1.9 and 2.0? A complete description of all the standard libraries available in Ruby 1.9 and 2.0, with examples of their usage.
Why should you learn Ruby 1.9 and 2.0? A wrap-up of the main benefits and advantages of using Ruby 1.9 and 2.0 for your programming projects.
FAQs
Here are some frequently asked questions about Ruby 1.9 and 2.0:
How can I check which version of Ruby I have installed?
You can check which version of Ruby you have installed by typing ruby -v in your terminal. This should output some information on the installed Ruby version, such as ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux].
How can I switch between different versions of Ruby on my system?
You can switch between different versions of Ruby on your system by using a Ruby version manager, such as RVM or rbenv. These tools allow you to install and manage multiple versions of Ruby on your machine and to specify which version to use for each project or directory.
What are some of the differences between Ruby 1.8 and Ruby 1.9 and 2.0?
Some of the differences between Ruby 1.8 and Ruby 1.9 and 2.0 are:
Ruby 1.9 and 2.0 use a new virtual machine called YARV that improves performance and compatibility.
Ruby 1.9 and 2.0 have new syntax and semantics features, such as keyword arguments, local variable scoping in blocks, encoding support for multilingual strings, etc.
Ruby 1.9 and 2.0 have new standard libraries that provide useful functionality for various tasks and domains, such as Enumerator::Lazy, Refinements, JSON, Psych, etc.
Where can I find more resources and support for learning and using Ruby 1.9 and 2.0?
You can find more resources and support for learning and using Ruby 1.9 and 2.0 online or offline, such as:
The official Ruby website at https://www.ruby-lang.org/en/, which provides documentation, downloads, news, community links, etc.
The official Ruby documentation at https://ruby-doc.org/, which provides reference manuals, tutorials, guides, books, etc.
The official Ruby forum at https://www.ruby-forum.com/, which provides a platform for asking and answering questions about Ruby.
The official Ruby mailing list at https://www.ruby-lang.org/en/community/mailing-lists/, which provides a way to communicate with other Ruby users via email.
The official Ruby IRC channel at https://www.ruby-lang.org/en/community/irc/, which provides a way to chat with other Ruby users in real time.
The Stack Overflow website at https://stackoverflow.com/questions/tagged/ruby, which provides a platform for asking and answering questions about programming in general and Ruby in particular.
The Ruby Gems website at https://rubygems.org/, which provides a repository of libraries and tools for Ruby.
The Ruby Toolbox website at https://www.ruby-toolbox.com/, which provides a catalog of libraries and tools for Ruby categorized by topics.
The Awesome Ruby website at https://awesome-ruby.com/, which provides a curated list of awesome libraries and tools for Ruby.
The various books, tutorials, blogs, podcasts, courses, conferences, meetups, etc., that are available online or offline for learning and using Ruby.
71b2f0854b