Learn ruby

来自linux中国网wiki
跳到导航 跳到搜索

fun

Ruby 之父:写 Ruby 时工作特别闲,总加班的人很难做出创造

https://blog.codinghorror.com/why-ruby/

IDE for ruby

怎么感觉配置完 vim 写ruby 好过emacs 呢 是em没配置好么 

mkdir -p ~/.vim/autoload ~/.vim/bundle && \

把https://tpo.pe/pathogen.vim 这个文件放到   ~/.vim/autoload/pathogen.vim 手工可能更加快因为原因你懂的 
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim



vim ~/.vimrc

if has('mouse')
    set mouse-=a
endif

execute pathogen#infect()
syntax on
filetype plugin indent on


sudo gem install rsense

#The Rsense server is started from the commandline with:
rsense start


emacs 见 emacs 入门 

Ruby programming

chapter2

 names = ["love","my love","xi","me"]
names = ["love","my love","xi","me"]
names.each do |name|
    if /love/ =~ name
        puts name
    end
end


#执行结果
 ruby   28.rb 
love
my love


chapter3

#!/usr/bin/env ruby 
filename = ARGV[0]
file = File.open(filename)
text = file.read 
print text 
file.close 

=begin
    
ruby  read_text.rb  h.rb 
#!/usr/bin/env ruby 
puts "hello world"    


cat h.rb 
#!/usr/bin/env ruby 
puts "hello world"
=end



Ruby 连接 Mysql - MySql2

Ruby 连接 Mysql - MySql2

ruby_tour

 sudo apt-get install ruby-full 

  ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux-gnu]
evan@myxps:~/lx/ssh/intra$ 


交互式 Ruby(IRb)

交互式 Ruby(IRb)为体验提供了一个 shell。在 IRb shell 内,您可以逐行立即查看解释结果。

这个工具会随着 Ruby 的安装自动带有,所以您不需要做其他额外的事情,IRb 即可正常工作。

只需要在命令提示符中键入 irb,一个交互式 Ruby Session 将会开始,


intra$ irb
irb(main):001:0> puts "hey evan"
hey evan
=> nil


* base 20分钟学会Ruby 

evan@myxps:~$ irb
irb(main):001:0>  3+2
=> 5
irb(main):002:0> 3*4
=> 12
irb(main):003:0> 3**2
=> 9
irb(main):004:0> Math.sqrt(9)
=> 3.0


方法 function 

rb(main):005:0> def h 
irb(main):006:1*   puts "hey ruby"
irb(main):007:0> end
=> :h
irb(main):008:0> h
hey ruby
=> nil


irb(main):009:1* def h(name)
irb(main):010:1*   puts "hello #{name}"
irb(main):011:0> end
=> :h

irb(main):012:0> h("eva")
hello eva
=> nil

irb(main):014:1* def h(name="world")
irb(main):015:1*   puts "hello #{name.capitalize}!"
irb(main):016:0> end 
=> :h

如果不输入参数,那么就会使用默认参数world。
irb(main):017:0> h "evan"
hello Evan!
=> nil
irb(main):018:0> h
hello World!
=> nil



irb(main):019:1* class Greeter
irb(main):020:2*   def initialize(name = "world")
irb(main):021:2*     @name = name 
irb(main):022:1*   end 
irb(main):023:2*   def say_hi
irb(main):024:2*     puts "HI #{@name}!"
irb(main):025:1*   end
irb(main):026:2*   def say_bye 
irb(main):027:2*     puts "bye #{@name}. come back soon."
irb(main):028:1*   end 
irb(main):029:0> end
=> :say_bye

这里有一个新的关键词class,图中定义了一个名叫Greeter的类,里面有一些方法。注意这里的name,现在是一个实例变量,它对于Greeter中的所有类都是可用的。




irb(main):013:0> g = Greeter.new("pat")
irb(main):014:0> g.say_hi
HI pat!
=> nil
irb(main):015:0> g.say_bye
bye pat. come back soon.
=> nil
irb(main):016:0> g.@name
Traceback (most recent call last):
        3: from /usr/bin/irb:23:in `<main>'
        2: from /usr/bin/irb:23:in `load'
        1: from /usr/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
SyntaxError ((irb):16: syntax error, unexpected instance variable)
g.@name
  ^~~~~


对象内部



rb(main):017:0> Greeter.instance_methods
=> [:say_hi, :say_bye, :dup, :itself, :yield_self, :then, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :clone, :display, :hash, :class, :singleton_class, :public_send, :method, :public_method, :singleton_method, :define_singleton_method, :extend, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :nil?, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :to_s, :__send__, :!, :==, :!=, :equal?, :__id__, :instance_eval, :instance_exec]




这里有好多方法,但我们只定义了两个方法,这里是怎么回事呢?这是对象Greeter的全部方法,一个完整的列表,包含了父类定义的一些方法。如果要列出定义在Greeter中的且不包含父类的方法,那就使用参数false:

 Greeter.instance_methods(false)
=> [:say_hi, :say_bye]


irb(main):019:0> g.respond_to?("say_hi")
=> true





官方quickstart

20分钟体验 Ruby had 2021年 10月 31日 星期日 09:49:12 CST

ruboo

 irb
irb(main):001:1* def hello
irb(main):002:1*   out = "hello world"
irb(main):003:1*   puts out 
irb(main):004:0> end 
=> :hello
irb(main):005:0> hello
hello world
=> nil

局部变量 

#!/usr/bin/ruby3
# 不能是ruby 3呀 

class Example
    VAR1 = 100
    VAR2 = 200
    def show 
        puts "第一个常量人值为 #{VAR1}"
        puts "第二个常量人值为 #{VAR2}"
    end 
end

#创建 对象 
object=Example.new()
object.show 

=begin 
ruby eva/VAR.rb 
第一个常量人值为 100
第二个常量人值为 200
=end


Ruby 范围(Range

Ruby 范围(Range)

https://www.runoob.com/ruby/ruby-encoding.html


here https://www.runoob.com/ruby/ruby-variable.html

Ruby 判断

https://www.runoob.com/ruby/ruby-decision.html


#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
x=1
if x > 2
    puts "x gt 2"
elsif x <= 2 and x!=0
    puts "x is 1"


else
    puts "无法得知x的值"
end 
=begin 

x is 1

=end

Ruby 循环

https://www.runoob.com/ruby/ruby-loop.html

Ruby 方法

Ruby 方法与其他编程语言中的函数类似。Ruby 方法用于捆绑一个或多个重复的语句到一个单元中。

方法名应以小写字母开头。如果您以大写字母作为方法名的开头,Ruby 可能会把它当作常量,从而导致不正确地解析调用。

方法应在调用之前定义,否则 Ruby 会产生未定义的方法调用异常。

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
 
def test(a1="Ruby", a2="Perl")
    puts "编程语言为 #{a1}"
    puts "编程语言为 #{a2}"
 end
 test "C", "C++"
 test

#=begin
 #编程语言为 C
 #编程语言为 C++
 #编程语言为 Ruby
 #编程语言为 Perl


#!/usr/bin/ruby3
# -*- coding: UTF-8 -*-
def test
    i = 100
    j = 300

return i ,j 
end 

var = test 
puts var

#100
#300


#可变数量的参数

#类方法

Ruby 块

Ruby 模块(Module)

https://www.runoob.com/ruby/ruby-module.html

Ruby 字符串(String)

https://www.runoob.com/ruby/ruby-string.html


irb(main):001:0> name1 = "evan"
=> "evan"
irb(main):002:0> name2 = "jay"
=> "jay"
irb(main):003:0> puts "hello #{name1}, #{name2} where?"
hello evan, jay where?

Ruby 数组(Array)

https://www.runoob.com/ruby/ruby-array.html

Ruby 哈希(Hash)

https://www.runoob.com/ruby/ruby-hash.html

Ruby 日期 & 时间(Date & Time)

https://www.runoob.com/ruby/ruby-date-time.html

 cat t.rb 
#!/usr/bin/ruby3 -w
# -*- coding: utf-8 -*-

time = Time.new

# Time 的组件
puts "当前时间 : " + time.inspect
puts time.year    # => 日期的年份
puts time.month   # => 日期的月份(1 到 12)
puts time.day     # => 一个月中的第几天(1 到 31)
puts time.wday    # => 一周中的星期几(0 是星期日)
puts time.yday    # => 365:一年中的第几天
puts time.hour    # => 23:24 小时制
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999:微秒
puts time.zone    # => "UTC":时区名称
evan@myxps:~/data/ruby/tmp/time$ ruby3 t.rb 
当前时间 : 2021-10-23 13:51:16.881564735 +0800
2021
10
23
6
296
13
51
16
881564
CST

Ruby 范围(Range)



果需要,您可以使用 to_a 方法把范围转换为列表。尝试下面的实例:

实例
#!/usr/bin/ruby
 
$, =", "   # Array 值分隔符
range1 = (1..10).to_a






https://www.runoob.com/ruby/ruby-range.html

Ruby 迭代器

单来说:迭代(iterate)指的是重复做相同的事,所以迭代器(iterator)就是用来重复多次相同的事。

迭代器是集合支持的方法。存储一组数据成员的对象称为集合。在 Ruby 中,数组(Array)和哈希(Hash)可以称之为集合。

迭代器返回集合的所有元素,一个接着一个。在这里我们将讨论两种迭代器,each 和 collect。

learn by video

03数字和文本

https://www.bilibili.com/video/BV1RJ411Y7A4?p=3

temp

https://www.bilibili.com/video/BV1RJ411Y7A4?p=3&spm_id_from=pageDriver

https://www.bilibili.com/video/BV1RJ411Y7A4?p=2

ruby and devops

devops/redis-6.2.5/src$ vim redis-trib.rb


ruby rails 做的运维管理系统

Rails 的 CMS 内容管理系统


ruby 爬虫爬取拉钩网职位信息,产生词云报告


一些自写Ruby脚本的合集


jenkins and ruby

https://rubygems.org/gems/jenkins_api_client/versions/1.5.3

https://joachim8675309.medium.com/jenkins-ci-pipeline-with-ruby-62017469c7c9


ruby rspec+jenkins+ci_report持续集成生成junit测试报告

ruby Rspec+jenkins+allure持续集成


Ruby API wrappers

https://www.jenkins.io/doc/book/using/remote-access-api/

ruby调用jenkins API使用

https://github.com/arangamani/jenkins_api_client

https://www.rubydoc.info/gems/jenkins_api_client

vim getjobs.rb

require 'jenkins_api_client'
 
@client = JenkinsApi::Client.new(:server_url => 'http://192.168.88.108:8080', :username=>'admin', :password=>'xxxdddddd')
job = @client.job 
puts(job.list_all)


运行结果 

01_Eureka
A-PHP
My-freestyle-job
My-pipeline-job
My-pipeline-job01




@client = JenkinsApi::Client.new(:server_ip => '0.0.0.0',
         :username => 'somename', :password => 'secret password')
# The following call will return all jobs matching 'Testjob'
puts @client.job.list("^Testjob")


ruby操作json

Ruby - iterate over parsed JSON

ruby json解析&生成

install

Building from Source

OS默认的是ruby2.7 想试一下3.0.2

 tar xvf ruby-3.0.2.tar.gz 
 cd ruby-3.0.2/

sudo mkdir -p  /usr/local/ruby3
 sudo ./configure  --prefix=/usr/local/ruby3

 sudo make -j6
  sudo make  install

sudo  ln -s /usr/local/ruby3/bin/ruby /usr/bin/ruby3
sudo  ln -s /usr/local/ruby3/bin/irb /usr/bin/irb3


see also

Jekyll

How to Install Ruby on Rails (RoR) on Debian 10

Rubyonrails基础

How to Install Ruby2.3 on centos7

references see also

w3cschool.cn Ruby 教程


runoob.com ruby-tutorial.html Ruby 教程

ruby学习


ruby连接linux操作系统shell


https://blog.csdn.net/mmiww/article/details/1363960


https://blog.csdn.net/mmiww/article/details/1355157


https://www.runoob.com/ruby/ruby-installation-unix.html

https://www.cnblogs.com/xuliangxing/p/7132656.html

https://linuxize.com/post/how-to-install-ruby-on-debian-9/

https://flicsdb.com/how-to-install-ruby-on-rails-on-ubuntu-or-kali-linux/






Ruby Introduction to Programming with Ruby - 给 Ruby 新手写的免费电子书

Ruby & Rails 书籍推荐

Ruby 源代码镜像服务

homeland. 开源、免费、不限制商业使用的社区论坛系统