原创编程基础之Ruby
Posted barneywill
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原创编程基础之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的主要内容,如果未能解决你的问题,请参考以下文章