编程基础之Ruby
ruby2.6.2
官方:https://www.ruby-lang.org/en/
一 简介
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
Ruby is a language of careful balance. Its creator, Yukihiro “Matz” Matsumoto, blended parts of his favorite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to form a new language that balanced functional programming with imperative programming.
Since its public release in 1995, Ruby has drawn devoted coders worldwide. In 2006, Ruby achieved mass acceptance. With active user groups formed in the world’s major cities and Ruby-related conferences filled to capacity.
ruby首次发布于1995年;在2006年,ruby开始被广泛接受;
Ruby is ranked among the top 10 on most of the indices that measure the growth and popularity of programming languages worldwide (such as the TIOBE index). Much of the growth is attributed to the popularity of software written in Ruby, particularly the Ruby on Rails web framework.
ruby一直位于编程语言top10,很多增长来自于使用ruby编写的软件,比如ruby on rails;
Initially, Matz looked at other languages to find an ideal syntax. Recalling his search, he said, “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.”
Ruby is seen as a flexible language, since it allows its users to freely alter its parts. Essential parts of Ruby can be removed or redefined, at will. Existing parts can be added upon. Ruby tries not to restrict the coder.
Ruby, as a language, has a few different implementations. This page has been discussing the reference implementation, in the community often referred to as MRI (“Matz’s Ruby Interpreter”) or CRuby (since it is written in C), but there are also others. They are often useful in certain situations, provide extra integration to other languages or environments, or have special features that MRI doesn’t.
ruby作为一种语言,有很多种不同的实现,比如cruby,jruby;
JRuby is Ruby atop the JVM (Java Virtual Machine), utilizing the JVM’s optimizing JIT compilers, garbage collectors, concurrent threads, tool ecosystem, and vast collection of libraries.
jruby是jvm上的ruby;
二 安装
1 使用网页版
https://ruby.github.io/TryRuby/
2 使用docker
$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp ruby:2.6.2 ruby your-daemon-or-script.rb
参考:https://hub.docker.com/_/ruby/
3 使用rbenv
# 1 install rbenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv $ cd ~/.rbenv && src/configure && make -C src $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ source ~/.bash_profile $ ~/.rbenv/bin/rbenv init
# 2 install ruby-build plugin $ mkdir -p "$(rbenv root)"/plugins $ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
# 3 install ruby $ rbenv install -l $ rbenv install 2.6.2
# 4 use ruby
$ rbenv shell 2.6.2
$ ruby -v
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux]
$ ruby test.rb
安装时可能报错:
$ rbenv install 2.6.2
Downloading ruby-2.6.2.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.2.tar.bz2
Installing ruby-2.6.2...BUILD FAILED (CentOS 7.4.1708 using ruby-build 20190314-4-g8e70553)
Inspect or clean up the working tree at /tmp/ruby-build.20190319152414.23377
Results logged to /tmp/ruby-build.20190319152414.23377.logLast 10 log lines:
installing html-docs: /root/.rbenv/versions/2.6.2/share/doc/ruby
installing capi-docs: /root/.rbenv/versions/2.6.2/share/doc/ruby
The Ruby readline extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y readline-devel` to fetch missing dependencies.
按照提示安装即可
yum install -y readline-devel
参考
https://github.com/rbenv/rbenv
https://github.com/rbenv/ruby-build
三 使用
$ cat /tmp/test.rb puts 'hello world' $ ruby /tmp/test.rb hello world
ruby有很多应用,比如logstash、ruby on rails等;
ruby api 参考:https://ruby-doc.org/core-2.6.2/
相关文章
- Ruby Programming | 连载 03 - Ruby 对象基础
- 开心档-软件开发入门之Ruby 数组(Array)
- 开心档-软件开发入门之Ruby CGI Cookie
- 开心档-软件开发入门之Ruby 块
- rvm,ruby的安装详解大数据
- Linux下Ruby版本的升级指南(linuxruby升级)
- 应用Linux下启动Ruby应用的指南(linux启动ruby)
- Ruby中循环引用导致lighttpd中的ruby fcgi脚本执行失败(Mac OS)
- Connecting Ruby to MySQL: A Comprehensive Guide for Developers(ruby连接mysql)
- 快速下载MySQLRuby,为Ruby开发提供强大支持(mysql_ruby下载)
- 如何正确安装Ruby搭配Redis(ruby安装 redis)
- 里的应用Ruby语言与Redis结合应用的一次尝试(ruby在redis)
- Ruby环境下Redis的快速安装(ruby redis安装)
- windows和linux下Ruby的下载与安装
- ruby杂项
- ruby程序的执行顺序
- 21个你应该知道的Ruby编程技巧
- Ruby中字符串左侧补零方法实例
- Ruby学习笔记一
- Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件