Learn ruby

来自linux中国网wiki
Evan讨论 | 贡献2021年10月17日 (日) 05:37的版本 →‎Ruby 循环
跳到导航 跳到搜索

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





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 会产生未定义的方法调用异常。

learn by video

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


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. 开源、免费、不限制商业使用的社区论坛系统