- 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
Ruby data types
In this part of the Ruby tutorial, we will talk about data types.
Computer programs of all sorts, including spreadsheets, text editors, calculators, and chat clients, work with data. Tools to work with various data types are essential part of a modern computer language. A data type is a set of values, and the allowable operations on those values.
Ruby has several data types. All data types are based on classes. The following are the data types recognized in Ruby:
- Booleans
- Symbols
- Numbers
- Strings
- Arrays
- Hashes
In the following example, we have all important Ruby data types.
#!/usr/bin/ruby h = { :name => "Jane", :age => 17 } p true.class, false.class p "Ruby".class p 1.class p 4.5.class p 3_463_456_457.class p :age.class p [1, 2, 3].class p h.class
We print their class names. A class is a template from each object is created.
p true.class, false.class
The boolean values are presented by true and false objects.
p "Ruby".class
This is the string.
p 1.class p 4.5.class p 3_463_456_457.class
These are the numbers.
p :age.class
This is a symbol, a data type specific to Ruby.
p [1, 2, 3].class p h.class
These are two containers, the array and the hash.
$ ./types.rb TrueClass FalseClass String Fixnum Float Bignum Symbol Array Hash
The program lists classes that belong to Ruby data types.
Boolean values
There is a duality built in our world. There is a Heaven and Earth, water and fire, jing and jang, man and woman, love and hatred. This is the 'boolean' nature of our existence. In Ruby the boolean data type can have one of the two values: true or false. Boolean is a fundamental data type: one that is very common in computer programs.
Happy parents are waiting a child to be born. They have chosen a name for both possibilities. If it is going to be a boy, they have chosen John. If it is going to be a girl, they have chosen Victoria.
#!/usr/bin/ruby # kid.rb bool = [true, false] male = bool[rand(2)] if male puts "We will use name John" else puts "We will use name Victoria" end
The program uses a random number generator to simulate our case.
bool = [true, false]
We have a bool
variable. It is an array of two boolean values. An array is created with square brackets.
male = bool[rand(2)]
We use a rand()
method to create a random number. The method returns either 0 or 1. The returned number is an index to the bool array.
if male puts "We will use name John" else puts "We will use name Victoria" end
Depending on the male variable, we print a message. If the male variable is set to true, we choose name John. Otherwise, we choose name Victoria. Control structures like if/else statements work with boolean values.
$ ./kid.rb We will use name Victoria $ ./kid.rb We will use name Victoria $ ./kid.rb We will use name John $ ./kid.rb We will use name John $ ./kid.rb We will use name John
Running the program several times.
Symbols
Symbols are used to represent other objects. Using symbols instead of strings may save some resources. A symbol is an instance object of a Symbol
class. Symbols are generated by using an colon before an identifier, like :name
. Several objects also have to_sym
methods. These methods convert those objects to symbols.
A Ruby symbol cannot be changed at runtime. Ruby symbols are often used as hash keys, because we do not need full capabilities of a string objects for a key.
#!/usr/bin/ruby p :name p :name.class p :name.methods.size p "Jane".methods.size p :name.object_id p :name.object_id p "name".object_id p "name".object_id
In the first example, we show some basic operations with Ruby symbols.
p :name p :name.class
We print a symbol and its class to the console. The class of the symbol is Symbol
.
p :name.methods.size p "Jane".methods.size
We compare the number of methods associated with instances of symbols and strings. A string has more than twice as many methods than symbol.
p :name.object_id p :name.object_id p "name".object_id p "name".object_id
Same symbols have the same id. Same strings have different ids.
$ ./symbols.rb :name Symbol 79 162 10328 10328 77344750 77344730
Sample output.
Symbols may be used as flags. Constants may be used in such situations as well. In C/C++ we would use enumerations.
#!/usr/bin/ruby light = :on if light == :on puts "The light is on" else puts "The light is off" end light = :off if light == :on puts "The light is on" else puts "The light is off" end
A light is either on or off. For both states we might define symbols.
light = :on
The light is on.
if light == :on puts "The light is on" else puts "The light is off" end
The logic of the program depends on the state of the light
variable.
Symbols are often used as keys in hash containers. They are more efficient that strings.
#!/usr/bin/ruby domains = {:sk => "Slovakia", :no => "Norway", :hu => "Hungary"} puts domains[:sk] puts domains[:no] puts domains[:hu]
In the script we have a domains hash. The keys in the hash are symbols.
puts domains[:sk] puts domains[:no] puts domains[:hu]
Keys are used to access values of a hash. Here we print three values of a hash.
$ ./symbols3.rb Slovakia Norway Hungary
Output of the example.
Ruby interpreter stores some references internally as symbols.
#!/usr/bin/ruby class Being def initialize @is = true end def say "I am being" end end b = Being.new p b.method :say p b.instance_variable_get :@is
A Being
class is defined. The class has a custom instance variable @is
and a say
method. These two entities are stored using symbols by Ruby.
p b.method :say
The method
method looks up a receiver method with a given name in the b
object. We look for a :say
symbol.
p b.instance_variable_get :@is
We use instance_variable_get
to check if @is
is an instance variable of the b
object. Internally the variable is stored as an :@is
symbol.
$ ./symbols4.rb #<Method: Being#say> true
Generated output.
All symbols are stored in a symbol table. In the next example, we look at the table. The all_symbols
method of a Symbol
class returns an array of all symbols from the table.
#!/usr/bin/ruby def info "info method" end @v = "Ruby" @@n = "16" p Symbol.all_symbols.include? :info p Symbol.all_symbols.include? :@v p Symbol.all_symbols.include? :@@n
A method, an instance variable and a class variable are created in a Ruby script. We check if these entities are stored in a symbol table.
p Symbol.all_symbols.include? :info
We check if the :info
symbol is in the symbol table. The line returns true.
$ ./symbols5.rb true true true
All three symbols are present in the Ruby symbol table.
Integers
Integers are a subset of the real numbers. They are written without a fraction or a decimal component. Integers fall within a set Z = {..., -2, -1, 0, 1, 2, ...} This set is infinite.
In computer languages, integers are primitive data types. In practice, computers can only work with a subset of integer values, due to computers' having finite capacity. Integers are used to count discrete entities. We can have 3, 4 or 6 humans, but we cannot have 3.33 humans. We can have 3.33 kilograms.
Integers are instance objects of a Fixnum
or a Bignum
class in Ruby. Unlike in languages like Java or C, integers in Ruby are objects. The two classes differ in size. Fixnum
numbers are integers up to a certain limit. The limit is machine dependent. Bignum
values hold integers outside the range of the Fixnum
. If any operation on a Fixnum
exceeds its range, the value is automatically converted to a Bignum
. The programmer usually does not need to care about the class type of the integers.
#!/usr/bin/ruby p -2 p 121 p 123265 p -34253464356 p 34867367893463476 p 1.class p 23453246.class p 234532423563456346.class p 2345324235632363463456456346.class p 5 / 2 p 5.div 2
In this example, we deal with integers.
p -2 p 121 p 123265 p -34253464356 p 34867367893463476
These are positive and negative integer values of various sizes.
p 1.class p 23453246.class p 234532423563456346.class p 2345324235632363463456456346.class
We print the classes of these integers. The first two integers are instances of a Fixnum
class. The other two are instances of a Bignum
class.
p 5 / 2 p 5.div 2
The two lines show integer division. When we divide two integers using the integer division operator/method, the result is an integer as well.
$ ./integers.rb -2 121 123265 -34253464356 34867367893463476 Fixnum Fixnum Bignum Bignum 2 2
Output of the example.
Integers can be specified in different notations in Ruby: decimal, hexadecimal, octal, and binary. Decimal numbers are used normally, as we know them. Hexadecimal numbers are preceded with 0x
characters, octal with 0
character and binary with 0b
characters.
#!/usr/bin/ruby puts 122 puts 0x7a puts 0172 puts 0b1111010
In the code example, we print decimal 122 in all these notation.
$ ./inotations.rb 122 122 122 122
Ouput of the example.
If we work with integers, we deal with discrete entities. We would use integers to count apples.
#!/usr/bin/ruby baskets = 16 apples_in_basket = 24 total = baskets * apples_in_basket puts "There are total of #{total} apples"
In our program, we count the total amount of apples. We work with integers.
$ ./apples.rb There are total of 384 apples
The output of the program.
Big numbers are difficult to read. If we have a number like 245342395423452, we find it difficult to read quickly. Outside computers, big numbers are separated by spaces or commas. For readability, Ruby allows integers to contain underscores. Underscores in integers are ignored by the Ruby interpreter.
#!/usr/bin/ruby p 23482345629 p 23_482_345_629 p 23482345629 == 23_482_345_629
The example demonstrates this use of underscores.
p 23482345629 == 23_482_345_629
This line shows that the two numbers are equal. It prints true.
$ ./underscore.rb 23482345629 23482345629 true
Example output.
Floating point numbers
Floating point numbers represent real numbers in computing. Real numbers measure continuous quantities like weight, height or speed. In Ruby, decimal numbers are objects of the Float
or a BigDecimal
class. The BigDecimal
class, a Ruby core class, is part of Ruby's standard library. In addition, we can use Rational
objects too.
We need to understand that decimal numbers are not precise. The official Ruby documentation clearly says that float objects represent inexact real numbers.
#!/usr/bin/ruby p 15.4 p 0.3455 p -343.4563 p 12.5.class p -12.5.class p (5.0 / 2).class p 5.fdiv 2 p 12.to_f
In the above program, we work with floating point values.
p 15.4 p 0.3455 p -343.4563
Here we print three decimal values. Decimal numbers have a decimal point character.
p 12.5.class p -12.5.class p (5.0 / 2).class
The above code lines show the types of the numbers. All are floats. Integer division applied on at least one Float
produces a Float
too.
p 5.fdiv 2 p 12.to_f
Here we create floating point values by using the floating point fdiv
division method and the conversion to_f
method.
$ ./decimals.rb 15.4 0.3455 -343.4563 Float Float Float 2.5 12.0
Output.
By default, a decimal number is shown with a maximum 16 numbers after the decimal point. We can control the format of floating point values with the sprintf
or printf
methods.
#!/usr/bin/ruby p 1/3.0 p 1.fdiv 2 puts sprintf "%.4f" % (1/3.0) puts sprintf "%.7f" % (5/3.0)
Formatting decimal numbers.
p 1/3.0 p 13.fdiv 4 p 1.fdiv 2
The first line prints a decimal with 16 places after the point. The second line prints two numbers after the point and the third one.
puts sprintf "%.4f" % (1/3.0) puts sprintf "%.7f" % (5/3.0)
Here we control the number of values after the decimal point using the sprintf
method. There is a precision in the format specifier of the sprintf
method. It is a number following the %
character. The f
is a conversion specifier that says we are dealing with floating point values.
$ ./formatfloat.rb 0.3333333333333333 3.25 0.5 0.3333 1.6666667
Output.
Ruby supports the use of scientific notation for floating point values. Also known as exponential notation, it is a way of writing numbers too large or small to be conveniently written in standard decimal notation.
#!/usr/bin/ruby p 1.2e-3 p 0.0012 p 1.5E-4 p 0.00015
The example shows two decimal numbers written in scientific notation.
$ ./scientific.rb 0.0012 0.0012 0.00015 0.00015
This is the output of the above program.
As we have already stated, floating point values are slightly inaccurate. For many computations, ordinary floating point numbers are sufficiently precise: e.g. if it is not that important if our weight is 60kg or 60.000023kg. For other computations, including many scientific and engineering applications, precision is paramount.
Ruby has a BigDecimal
in the standard library. This class provides arbitrary precision for very large or very accurate floating point numbers.
#!/usr/bin/ruby require 'bigdecimal' sum = 0 1000.times do sum = sum + 0.0001 end p sum sum = BigDecimal.new("0") 1000.times do sum = sum + BigDecimal.new("0.0001") end puts sum.to_s('F') puts sum.to_s('E')
In this simple example, we compare the precision of a Float
compared to a BigDecimal
.
require 'bigdecimal'
The BigDecimal
class must be imported.
sum = 0 1000.times do sum = sum + 0.0001 end p sum
We form a loop, where we add a small floatig point value to a sum variable. In the end, there will be a small inaccuracy.
sum = BigDecimal.new("0") 1000.times do sum = sum + BigDecimal.new("0.0001") end
We do the same loop with the BigDecimal
values.
puts sum.to_s('F') puts sum.to_s('E')
The sum is printed in floting point and engineering notation.
$ ./bigdecimal.rb 0.10000000000000184 0.1 0.1E0
The output shows that the computing with BigDecimal
is more precise than with Floats
.
Let's say a sprinter for 100m ran 9.87s. What is his speed in km/h?
#!/usr/bin/ruby distance = 0.1 time = 9.87 / 3600 speed = distance / time puts "The average speed of a sprinter is #{speed} km/h"
In this example, it is necessary to use floating point values.
distance = 0.1
100m is 0.1 km.
time = 9.87 / 3600
9.87s is 9.87/60*60 h
speed = distance / time
To get the speed, we divide the distance by the time.
$ ./speed.rb The average speed of a sprinter is 36.4741641337386 km/h
This is the output of the speed.rb
script.
Rational Numbers
Ruby supports rational numbers. A rational number is an exact number. Using rational numbers we avoid rounding errors. In Ruby, a rational number is an object of the Rational
class. We can create rational numbers with a special to_r
method from some objects.
A rational number is any number that can be expressed as a fraction of two integers a/b , where b!=0. Since b may be equal to 1, every integer is a rational number.
#!/usr/bin/ruby puts 2.to_r puts "23".to_r puts 2.6.to_r p Rational 0 p Rational 1/5.0 p Rational 0.5
This example shows a few rational numbers.
puts 2.to_r
Here we convert a 2 integer to 2/1 rational number using the to_r
method.
p Rational 0.5
We create a rational number with the Rational
class.
$ ./rational.rb 2/1 23/1 5854679515581645/2251799813685248 (0/1) (3602879701896397/18014398509481984) (1/2)
Output of the example.
The nil value
Ruby has a special value nil
. It is an absence of a value. The nil
is a singleton object of a NilClass
. There is only one nil
; we cannot have more of it.
#!/usr/bin/ruby puts nil p nil p $val p [1, 2, 3][4] p $val1 == $val2
An example with the nil
value.
puts nil p nil
We print the nil
value to the console. The puts
method prints an empty string; the p
method prints 'nil' string.
p $val
When we refer to a global variable that was not set, we get the nil
value.
p [1, 2, 3][3]
In this code line, we refer to the fourth element of a three-element array. We get nil
. Many methods in Ruby return nil
for invalid values.
p $val1 == $val2
The line returns true. This is a consequence of the fact that the nil
value is a singleton object of a NilClass
.
$ ./nilvalue.rb nil nil nil true
Output.
Strings
A string is a data type representing textual data in computer programs. A Ruby string is a sequence of unicode characters. A string is an instance of the String
. String literals are characters enclosed in double or single qoutes.
A string is a very important data type. It deserves a dedicated chapter. Here we include just a small example.
#!/usr/bin/ruby p "Ruby" p 'Python' p "Ruby".size p "Ruby".upcase p 23.to_s
In this program, we work with Ruby strings. We use the p
method for printing because we see the data type on output.
p "Ruby" p 'Python'
We print two string literals to the terminal. The first literal is enclosed in double quotes, the second literal in single quotes.
p "Ruby".size p "Ruby".upcase
These two lines call two string methods. The size
method returns the size of the string: 4 characters in our case. The upcase
returns the string in uppercase letters.
p 23.to_s
The to_s
method converts an integer to a string.
$ ./strings.rb "Ruby" "Python" 4 "RUBY" "23"
In the output we see strings enclosed in quotes. This is the consequence of using the p
method. The print
and puts
methods don't do this.
Arrays and hashes
Arrays and hashes are collections of objects. They group objects into one place.
Arrays are ordered collections of objects. Hashes are collections of key-value pairs. We will have a single chapter for both arrays and hashes. The following example just gives a quick look at both containers.
#!/usr/bin/ruby nums = [1, 2, 3, 4] puts "There are #{nums.size} items in the array" nums.each do |num| puts num end domains = { :de => "Germany", :sk => "Slovakia", :us => "United States", :no => "Norway" } puts domains.keys puts domains.values
This is a quick example for a Ruby array and hash.
nums = [1, 2, 3, 4] puts "There are #{nums.size} items in the array" nums.each do |num| puts num end
These lines create an array having 4 items. In the second line we count the items of the array and incorporate it in the message. Later we go through the array with the each
method and print each of the elements to the console.
domains = { :de => "Germany", :sk => "Slovakia", :us => "United States", :no => "Norway" } puts domains.keys puts domains.values
Here we create a Ruby hash. Then we print its keys and values.
$ ./arrayshashes.rb There are 4 items in the array 1 2 3 4 de sk us no Germany Slovakia United States Norway
Example output.
Conversions
We often work with multiple data types at once. Converting one data type to another one is a common job in programming. Type conversion or typecasting refers to changing an entity of one data type into another. There are two types of conversion: implicit and explicit. Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. Ruby has only explicit conversion.
Ruby has built-in conversion methods like to_i
, to_s
or to_f
. The Kernel
module has a few public methods for doing conversions, like Integer
, String
or Float
. These methods should not be confused with Ruby classes.
#!/usr/bin/ruby p Array(1..6) p Complex 6 p Float 12 p Integer "34" p Rational 6 p String 22
Here we show the Kernel
conversion methods.
$ ./convertmethods.rb [1, 2, 3, 4, 5, 6] (6+0i) 12.0 34 (6/1) "22"
Output of the example.
#!/usr/bin/ruby p "12".to_i p 12.5.to_i p nil.to_i p 12.to_f p "11".to_f p nil.to_f
In the above example, we show some numerical conversions. Some Ruby objects have to_i
and to_f
methods which convert objects to integers and floats, respectively.
p "12".to_i p 12.5.to_i p nil.to_i
In this code we convert a string, decimal, and nil
to integer type.
p 12.to_f p "11".to_f p nil.to_f
These three lines convert an integer, string, and nil
to an object of decimal data type.
$ ./conversions.rb 12 12 0 12.0 11.0 0.0
Example output.
The second example shows some string conversions.
#!/usr/bin/ruby p "12".to_i p "13".to_f p "12".to_r p "13".to_c p "Jane".to_sym v = "Ruby Python Tcl PHP Perl".split p v.class
In the above example we convert strings to objects of different data types.
p "12".to_i p "13".to_f p "12".to_r p "13".to_c
Here strings are converted to integer, decimal, rational, and complex numbers.
p "Jane".to_sym
A string becomes a symbol.
v = "Ruby Python Tcl PHP Perl".split p v.class
Here we use a split
method of the String
class to convert a string to an array.
$ ./stringconv.rb 12 13.0 (12/1) (13+0i) :Jane Array
And this is what we get.
The next small example shows array hash conversions.
#!/usr/bin/ruby h = {:de => "Germany", :sk => "Slovakia"} p h.to_a a = [:de, "Germany", :sk, "Slovakia", :hu, "Hungary", :no, "Norway"] p Hash[*a]
In the example code, we create a hash and covert it to array. Then we create an array and convert it to a hash.
h = {:de => "Germany", :sk => "Slovakia"} p h.to_a
A hash is created and converted to an array using the to_a
method.
a = [:de, "Germany", :sk, "Slovakia", :hu, "Hungary", :no, "Norway"] p Hash[*a]
An array is created and converted to a hash. The asterisk in this context is a splat operator. It is one of the Ruby idioms taken from Perl. It splits an array into a few variables.
$ ./h2a.rb [[:de, "Germany"], [:sk, "Slovakia"]] {:de=>"Germany", :sk=>"Slovakia", :hu=>"Hungary", :no=>"Norway"}
Output of the example.
In this part of the Ruby tutorial, we covered data types and their conversions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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