返回介绍

Ruby

发布于 2025-02-22 22:19:58 字数 3872 浏览 0 评论 0 收藏 0

In this part of the Ruby tutorial, we will introduce the Ruby programming language.

Goal

The goal of this tutorial is to get you started with the Ruby programming language. The tutorial covers the core of the Ruby language, including variables, expressions, collections, control structures and other core features. It also describes some more advanced concepts like object-oriented programming and regular expressions. It is not a complete coverage of the language. The tutorial was created on Ubuntu Linux.

Ruby

Ruby logo Ruby is a dynamic, reflective, general-purpose object-oriented programming language. The original author is a Japanese programmer Yukihiro Matsumoto. Ruby first appeared in 1995.

Ruby supports various programming paradigms. This includes object orientation, reflection, imperative and reflective programming. Ruby language was influenced primarily by Perl, Smalltalk, Eiffel, and Lisp. Unlike languages like Java, C# or C, Ruby has no official specification. Instead the original C implementation of the Ruby language serves as a de facto reference. There are other implementations of the Ruby language like JRuby, IronRuby, or MacRuby.

The official web site is ruby-lang.org .

Popularity

There are hundreds of programming languages in use today. Ruby belongs to the most popular ones. The langpop.com and tiobe sites put Ruby around the 10th place. Ruby on Rails, a very popular web application framework is the first killer application created in Ruby.

Interactive interpreter

We can run Ruby statements in a script or in an interactive interpreter. In this tutorial, we will use the interactive Ruby session to demonstrate some smaller code fragments. Larger code examples are to be put in Ruby scripts.

$ irb
irb(main):001:0> puts RUBY_VERSION
1.8.7
=> nil

This is an example of the Ruby interactive session. We print the value of a special RUBY_VERSION constant to the console. It is set to the version of the current Ruby in use.

Ruby scripts

We will have our first simple example of a Ruby script.

#!/usr/bin/ruby

# first.rb 

puts "This is Ruby"

In this script, we print a message to the console.

#!/usr/bin/ruby

Every script in the UNIX starts with a shebang. The shebang is the first two characters in the script: #!. The shebang is followed by the path to the interpreter, which will execute our script. The /usr/bin/ is the most common location for the Ruby interpreter. It could also be located in /usr/local/bin/ or elsewhere.

# first.rb 

Comments in Ruby are preceded by a # character.

puts "This is Ruby"

The puts method prints a string to the console.

$ which ruby
/usr/bin/ruby

The path to the Ruby interpreter can be found using the which command.

$ chmod +x first.rb 
$ ./first.rb 
This is Ruby

We make the script executable with the chmod command and execute it.

Sources

The following sources were used to create this tutorial:

In this part of the Ruby tutorial, we have introduced the Ruby language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文