- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Objects in Ruby
In this part of the Ruby tutorial, we will briefly cover the concept of objects in Ruby language. We will learn more about objects in OOP chapter. I have written this preliminary chapter about objects because many Ruby features might be confusing to newcomers — especially if they already know any other programming language.
Ruby is an object-oriented programming language. This means that in Ruby programs we work with objects. From a language programmer's point of view a Ruby program is a stream of tokens. These tokens are Ruby keywords, operators, various delimiters or literals. From a semantic point of view a Ruby program consists of objects. These objects are created and modified during the lifetime of a Ruby script.
There are two kinds of objects: built-in objects and custom objects. Built-in objects are predefined objects that all programmers can use. They are available with the core of the Ruby language or from various libraries. Custom objects are created by application programmers for their application domains.
All objects must be created before we can work with them. We often use a term object instantiation. It is a synonym for object creation. Objects consists of data and methods. Data is a static part of an object. Methods form a dynamic part of an object. Objects are modified and communicate with each other via methods.
#!/usr/bin/ruby puts "Ruby language"
We have a simple Ruby script. If we are familiar with some procedural language like Pascal or C, we might see a keyword or a function named puts
and its parameter "Ruby language", which is a string.
Ruby is a pure object-oriented language and things are a bit different. The "Ruby language" is a indeed a string, which is a common data type. But it is also an object. And as with all objects, we can call its methods. This is a bit different from other languages. The puts
is a method. A method is a function defined in an object. Methods do not exist on their own. In fact, the puts
method is a part of the Kernel
module.
#!/usr/bin/ruby Kernel.puts "Ruby language" Kernel.puts "Ruby language".size
In the above script, we have two code lines.
Kernel.puts "Ruby language"
In the first example, we were calling the puts
method without the Kernel
part, which may be omitted. This saves time and some typing. It is in fact a shorthand call for the Kernel.puts
formal call. In C# we have Console.writeln
and in Java System.println
. The idea is the same. Methods must be associated with some object, or, in case of class methods, a class.
Kernel.puts "Ruby language".size
In this code line, we print the size of the "Ruby language" string to the console. This might be confusing to programmers who have coded in other languages. In other languages, a string is a primitive data type that cannot be modified and that lacks its own methods. In Ruby, a string is a full object and has its own methods. The size
method is one of them. It returns the size of the string in characters.
$ ./simple2.rb Ruby language 13
Output of the code example.
In the following example, we will look at an integer number. Similarly to a string, an integer value is a Ruby object too.
#!/usr/bin/ruby puts 6.object_id puts 6.even? puts 6.zero? puts 6.class
In the example, we have an integer 6. We call a few methods on the number.
puts 6.object_id
The 6 is an object. The object_id
is a method. The method returns an id associated to the object. Each object has an id. If we call a method on an object, we must always put a dot character between the two.
puts 6.even? puts 6.zero?
Here we call two methods on the 6 object. The even?
returns true if the number is even. And the zero?
method returns true if the number is equal to zero. Note that these two methods end with a question mark. This is a Ruby convention. Methods that return a boolean value end with a question mark.
puts 6.class
The class
method tells us what kind of object we are dealing with. In our case a 6 is a Fixnum
$ ./objectnumber.rb 13 true false Fixnum
Code example output.
Object creation
We have mentioned that Ruby objects must be created before we can work with them. Objects can be created implicitly or explicitly. Implicit object creation is object creation by literal notation. Explicit object creation happens with the use of the new
keyword. A custom object is always created with the new
keyword. Custom objects must be created from a particular class. A class is a template for an object. A class can be used to create many objects.
#!/usr/bin/ruby class Being end puts 67 puts "ZetCode" s = String.new "ZetCode" puts s # n1 = Fixnum.new 67 # puts n1 b = Being.new puts b
The code example demonstrates creation of objects in Ruby.
class Being end
This is a template for our custom object called Being
. The templates are created using the class
keyword. The templates for custom objects are usually placed at the top of the source file or in a separate Ruby files.
puts 67 puts "ZetCode"
In these two lines we work with two objects. A 67 object of Fixnum
type and "ZetCode" string of String
type. 67 and "String" are what we call literals. A literal is a textual representation of a particular value of a type. These two objects are created behind the scenes by the Ruby interpreter. Some objects in Ruby are created by specifying their literals in the source code.
s = String.new "ZetCode" puts s
This is the formal way of creating a String
object. It is equal to the previous, implicit creation with the string literal.
# n1 = Fixnum.new 67 # puts n1
Not all built-in objects can be created with the new
method. This code does not compile. Fixnum
numbers can be created only by the literal notation so far.
b = Being.new puts b
And here we create an instance of the custom object. The puts
method gives us a short description of the object.
$ ./ocreation.rb 67 ZetCode ZetCode #<Being:0x9944d9c>
Output of the example.
We will continue with some formal object creations.
#!/usr/bin/ruby s1 = String.new "Ruby" puts s1.size puts s1.downcase a1 = Array.new a1.push 1, 2, 3 puts a1.include? 3 puts a1.empty? r1 = Range.new 1, 6 puts r1.class puts r1.include? 4
In the example, we create three built-in objects and call a few of their methods.
s1 = String.new "Ruby" puts s1.size puts s1.downcase
A String
object is created. We call two methods of the object. The size
method returns the size of the string. The downcase
method downcases the characters of the string.
a1 = Array.new a1.push 1, 2, 3 puts a1.include? 3 puts a1.empty?
Here we create an Array
object and add three numbers to it. Later we call two array methods. The include?
method checks if a particular value (3 in our case) is part of the array. The empty?
method returns a boolean value indicating whether the array is empty.
r1 = Range.new 1, 6 puts r1.class puts r1.include? 4
An instance of the Range
class is created. It contains numbers from 1 to 6. The class
method returns the name of the object. The include?
method checks if the number 4 is part of the range. It is in our case.
$ ./formal.rb 4 ruby true false Range true
Running the example gives this output.
Object literals
As we have already mentioned, some built-in objects can be created using object literals. The following example shows several object literals.
#!/usr/bin/ruby 4.times { puts "Ruby" } puts "Ruby".size puts "Ruby".downcase puts [1, 2, 3].include? 3 puts [1, 2, 3].empty? puts :name.class puts :name.frozen? puts (1..6).class puts (1..6).include? 4
In the above example we use literal notation to create a fixnum, strings, arrays, symbols, and ranges.
4.times { puts "Ruby" }
We can immediately call a method on an integer literal. This line prints a "Ruby" string four times to the terminal.
puts "Ruby".size puts "Ruby".downcase
We call two methods on a String
object created with a string literal.
puts [1, 2, 3].include? 3 puts [1, 2, 3].empty?
Here we create two Array
objects using array literal notations. We check if a specific number is part of the array with the include?
method. The empty?
method checks if the array object is empty or not.
puts :name.class puts :name.frozen?
Two methods of the Symbol object are called. The symbol is created with a symbol literal, which starts with a colon.
puts (1..6).class puts (1..6).include? 4
Two Range
objects are created using the range literal. We call two methods on those objects. The class
method returns the name of the class and the include?
method checks if a given number is part of the range.
$ ./literals.rb Ruby Ruby Ruby Ruby 4 ruby true false Symbol false Range true
Example output.
Object hierarchy
In many object-oriented languages objects form a hierarchy. Ruby has and object hierarchy too. It is a tree-like hierarchy, where we have parent objects and child objects. Objects inherit data and behaviour from their parent objects. At the top of the hierarchy there is the root object. It is called the Object
. Each object in Ruby has at least one parent. In other words, every object inherits from the basic Object
object.
According to the official Ruby documentation, Object
is the root of Ruby's class hierarchy. Its methods are available to all classes unless explicitly overridden.
#!/usr/bin/ruby puts 4.is_a? Object puts "Ruby".is_a? Object puts [2, 3].is_a? Object puts :name.is_a? Object puts (1..2).is_a? Object
In the above code example we demonstrate that all objects inherit from the root Object
puts 4.is_a? Object
We use the is_a?
method to check if a number is a specific type: in other words, if it inherits from a given object type.
$ ./mother.rb true true true true true
All methods return true, which means that all objects inherit from the mother object.
The inheritance hierarchy may be quite complex even for the very basic Ruby objects.
#!/usr/bin/ruby puts 6.class puts 6.is_a? BasicObject puts 6.is_a? Object puts 6.is_a? Numeric puts 6.is_a? Integer puts 6.is_a? Fixnum puts 6.is_a? Bignum puts 6.is_a? String
In this example we shed some light on the inheritance hierarchy of a small numerical value.
puts 6.class
We find out what kind of object is the number value 6. The line prints Fixnum
to the console.
puts 6.is_a? BasicObject puts 6.is_a? Object puts 6.is_a? Numeric puts 6.is_a? Integer puts 6.is_a? Fixnum
All the above lines return true. Number 6 is a Fixnum
. From the Ruby documentation we find out that the four other objects are parents of the Fixnum
object.
puts 6.is_a? Bignum puts 6.is_a? String
The above two objects are not parents for the 6 value.
$ ./inheritance.rb Fixnum true true true true true false false
Output of the example.
We will finish this section with an example, demonstrating inheritance of custom user objects.
#!/usr/bin/ruby class Being def to_s "This is Being" end def get_id 9 end end class Living < Being def to_s "This is Living" end end l = Living.new puts l puts l.get_id puts l.is_a? Being puts l.is_a? Object puts l.is_a? BasicObject
In the example we create two objects, Being
and Living
. The Living
object inherits from the Being
. The first is a parent object and the second is a child object.
class Being def to_s "This is Being" end def get_id 9 end end
This is a definition of a custom Ruby object. The definition is placed between the class
and end
keywords. Inside the definition, we create two methods. When the puts
method takes an object as a parameter, it calls its to_s
method. It usually gives a string representation/description of the object.
class Living < Being def to_s "This is Living" end end
We create a definition of the Living
object. The object inherits from the Being
object. The < operator is used to create inheritance relationships. The to_s
method is overwritten.
l = Living.new
From the above Living
object template, we create an instance of the Living
object. The instance of a custom object is created with the new
keyword.
puts l
The puts
method calls the to_s
method of the Living
object. Had the to_s
method not been defined in the Living
class, the to_s
method of the Being
class would have been called.
puts l.get_id
The Living
object has no get_id
method defined. In such a case, the parent classes are checked if there is such a method. In our case the Being
method has such a method and it is called.
puts l.is_a? Being
The line returns true. The Living
is a type of a Being
; e.g. it inherits from the Being
class.
puts l.is_a? Object puts l.is_a? BasicObject
For our Living
custom object, we have not explicitly specified any relation to the Object
or BasicObject
objects. Yet the two lines return true. This is because every object in Ruby is automatically a descendant of these two objects. This is done behind the scenes by the Ruby interpreter.
$ ./custominher.rb This is Living 9 true true true
Output of the example.
Ruby toplevel
Ruby has a specific object referred to as Ruby toplevel. It is a default execution environment defined outside any other context like class or module definition. The toplevel's name is main
. It is an instance of the Object
type. There is a local space associated with main, where all local variables reside.
#!/usr/bin/ruby n1 = 3 n2 = 5 puts local_variables Kernel.puts self puts self.class
This is the first example describing the Ruby toplevel.
n1 = 3 n2 = 5
Here we have defined two numeric variables. These variables are local to the toplevel.
puts local_variables
Here we produce a list of all local variables. The local_variables
is a method of the Kernel
module, which is mixed into each Object
, including the toplevel object.
Kernel.puts self
The self
is a Ruby pseudo variable. It returns the current object receiver. The line prints "main" to the console. It is the name for the Ruby toplevel. The Kernel
part of the Kernel.puts
code can be omitted. By fully specifying the name we show that the puts
method belongs to the Kernel
module.
puts self.class
The line prints the class
of the toplevel. We get the object type of the toplevel. It is the Object
, which is the root of Ruby's class hierarchy.
$ ./toplevel.rb n1 n2 main Object
This is the output of the example. The n1
and n2
are the local variables associated with toplevel. The main is the name given for the Ruby toplevel execution environment. Finally, Object is the type of the toplevel.
We will have another example related to the Ruby toplevel.
#!/usr/bin/ruby @name = "Jane" @age = 17 def info "#{@name} is #{@age} years old" end puts self.instance_variables puts self.private_methods.include? :info puts info
We show instance variables and methods that belong to the toplevel environment.
@name = "Jane" @age = 17
We define two instance variables. Instance variables begin with the @
character in Ruby. Instance variables belong to a specific object instance. In this case, they belong to the Ruby toplevel.
def info "#{@name} is #{@age} years old" end
This is a method definition. Each method must belong to some object. This method belongs to the toplevel object. All toplevel methods are private. Access to private methods is restricted.
puts self.instance_variables
The instance_variables
method prints all instance variables of the self, which points to the Ruby toplevel in this context.
puts self.private_methods.include? :info
All toplevel methods are automatically private. The private_methods
returns all private methods of the object. Since there are many methods, we call the include?
method to check if the info method is among them. Note that we refer to the info method by its symbolic name.
$ ./toplevel2.rb @name @age true Jane is 17 years old
Example output.
This chapter covered some basics of the objects in Ruby language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论