1 / 8

Object Orientation in Ruby

Object Orientation in Ruby. Molly Baker CSCI 431. About Ruby. Ruby is a pure object-oriented language; everything in Ruby is an object.

feo
Download Presentation

Object Orientation in Ruby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Orientation in Ruby Molly Baker CSCI 431

  2. About Ruby • Ruby is a pure object-oriented language; everything in Ruby is an object. • Ruby was created from a blend of Perl, Smalltalk, Eiffel, Ada, and Lisp to form a flexible, natural language that balances functional and imperative programming. • Ruby creator, Yukihiro Matsumoto, wanted a flexible language, where users could freely alter whatever they deemed necessary. Ruby’s main motivation is to not restrict the coder and minimize effort in programming.

  3. Classes and Modules • A Ruby object has three components: a set of flags, instance variables, and an associated class. • To use a class, define an object. Allows access to its methods and variables. Include a module in the class, and you can use all the module’s methods. • Modules --group code by topic. • Modules similar to classes in that they hold a collection of methods and constants. Defined using the module keyword instead of class. Can’t create objects based on modules. Must first add it to a class. • Modules act as a namespace, letting you define methods whose names will not clash with those defined elsewhere.

  4. Inheritance in Ruby • Unlike many other object-oriented languages, Ruby allows only for single inheritance. • Inheritance indicated by a <. • A class cannot inherit from more than one class, but you can include more than one module in a class.

  5. Inheritance (Continued) • Classes can use modules and receive all their methods, by simply putting the include (module) statement in the class. This is called a mix-in. • Example: class MyArray    include Enumerable end • The reason for this is improved clarity and less restriction.

  6. In languages such as C++ and Java, class definitions are processed at compile time. • In Ruby, class and module definitions are executable code. Classes and modules are created at runtime, when the definition is encountered. This allows you to structure programs more dynamically than in most conventional languages.

  7. Examples of Ruby • Method for Hello World program def hputs "Hello World!“ endnil • Module: module Trig   PI = 3.1416    # class methods   def Trig.sin(x)     # ...   end def Trig.cos(x)      # ...   end end

  8. Sources • http://www.ruby-lang.org/en/about/ • http://ruby-doc.org/docs/ProgrammingRuby/html/classes.html • http://rubylearning.com

More Related