“Learn ruby”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第1行: 第1行:
 +
=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>
 +
 +
 +
  
  
 
=see also=
 
=see also=
 +
 +
 +
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/
 +
 +
 +
 +
 +
 +
 +
 +
  
  

2020年11月9日 (一) 11:49的版本

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




see also

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