查看“Learn ruby”的源代码
←
Learn ruby
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
=ruby_tour= <pre> 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 </pre> ==ruboo== <pre> 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 </pre> === Ruby 范围(Range=== [https://www.runoob.com/ruby/ruby-range.html 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 <pre> #!/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 </pre> ===Ruby 循环=== https://www.runoob.com/ruby/ruby-loop.html ===Ruby 方法=== Ruby 方法与其他编程语言中的函数类似。Ruby 方法用于捆绑一个或多个重复的语句到一个单元中。 方法名应以小写字母开头。如果您以大写字母作为方法名的开头,Ruby 可能会把它当作常量,从而导致不正确地解析调用。 方法应在调用之前定义,否则 Ruby 会产生未定义的方法调用异常。 <pre> #!/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 #可变数量的参数 #类方法 </pre> ===Ruby 块=== ===Ruby 模块(Module)=== https://www.runoob.com/ruby/ruby-module.html === Ruby 字符串(String)=== https://www.runoob.com/ruby/ruby-string.html <pre> 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? </pre> ==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 内容管理系统 [https://www.cnblogs.com/wf0117/p/9218196.html ruby 爬虫爬取拉钩网职位信息,产生词云报告 ] [http://xiajian.github.io/2014/10/10/some-ruby-scripts 一些自写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 [https://www.cnblogs.com/wf0117/p/9964807.html ruby rspec+jenkins+ci_report持续集成生成junit测试报告 ] [https://www.cnblogs.com/wf0117/p/9969005.html ruby Rspec+jenkins+allure持续集成 ] ===Ruby API wrappers=== https://www.jenkins.io/doc/book/using/remote-access-api/ [https://www.cnblogs.com/alison-lxj/p/10266006.html ruby调用jenkins API使用] https://github.com/arangamani/jenkins_api_client https://www.rubydoc.info/gems/jenkins_api_client <pre> 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") </pre> =ruby操作json= [https://stackoverflow.com/questions/22132623/ruby-iterate-over-parsed-json Ruby - iterate over parsed JSON] [https://www.cnblogs.com/wf0117/p/8854899.html ruby json解析&生成] =install= ==Building from Source== OS默认的是ruby2.7 想试一下3.0.2 <pre> 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 </pre> =references see also= [https://www.w3cschool.cn/ruby/ w3cschool.cn Ruby 教程] [https://www.runoob.com/ruby/ruby-tutorial.html runoob.com ruby-tutorial.html Ruby 教程] [https://www.cnblogs.com/wf0117/category/1040459.html?page=3 ruby学习] [https://blog.csdn.net/lijie1010/article/details/79001115 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/ [https://ruby-china.org/topics/19914 Ruby Introduction to Programming with Ruby - 给 Ruby 新手写的免费电子书 ] [https://ruby-china.org/wiki/books Ruby & Rails 书籍推荐] [https://ruby-china.org/wiki/ruby-mirror Ruby 源代码镜像服务] [https://homeland.ruby-china.org/ homeland. 开源、免费、不限制商业使用的社区论坛系统] [[category:ops]] [[category:ruby]]
返回至
Learn ruby
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
我的导航
关于我
shell
python
ops
linuxchina.net
blog.linuxchina
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息