ruby 源码

简介: 脚本 rb1021.rb -----------------------------------------------------------------------------------------   1 #!/usr/bin/ruby   2 # ruby code   3   4 class MegaGreeter   5     attr_accessor :n

脚本 rb1021.rb

-----------------------------------------------------------------------------------------

  1 #!/usr/bin/ruby
  2 # ruby code
  3
  4 class MegaGreeter
  5     attr_accessor :names
  6
  7     # Create the object
  8     def initialize(names = "world")
  9         @names = names
 10     end
 11
 12     # Say hi to everybody
 13     def say_hi
 14         if @names.nil?
 15             puts "..."
 16         elsif @names.respond_to?("each")
 17
 18             # @names is a list of some kind, iterate!
 19             @names.each do |name|
 20                 puts "Hello #{name}!"
 21             end
 22         else
 23             puts "Hello #{@names}!"
24         end
 25     end
 26
 27     # Say hi to everybody
 28     def say_bye
 29         if @names.nil?
 30             puts "..."
 31         elsif @name.respond_to?("join")
 32             # join the list elements with commas
 33             puts "Goodbye #{@names.join(", ")}. come back soon!"
 34         else
 35             puts "Goodbye #{@names}. come back soon!"
 36         end
 37     end
 38 end
 39
 40 if __FILE__ == $0
 41     mg = MegaGreeter.new
 42     mg.say_hi
 43     mg.say_bye
 44
 45     # Change name to be "Zeke"
 46     mg.names = "Zeke"
 47     mg.say_hi
 48     mg.say_bye
 49
 50     # Change the name to an array of names
 51     mg.names = ["Albert", "Brenda", "Charles", "Dave", "Englebert"]
 52     mg.say_hi
 53     mg.say_bye
 54
 55     # Change to nil
 56     mg.names = nil
 57     mg.say_hi
 58     mg.say_bye
 59 end

-----------------------------------------------------------------------------------------

输出

-----------------------------------------------------------------------------------------

[embedded@embedded ruby]$ ruby rb1021.rb
Hello world!
Goodbye world. come back soon!
Hello Zeke!
Goodbye Zeke. come back soon!
Hello Albert!
Hello Brenda!
Hello Charles!
Hello Dave!
Hello Englebert!
Goodbye AlbertBrendaCharlesDaveEnglebert. come back soon!
...
...

-----------------------------------------------------------------------------------------

脚本源码

看一下 bash python ruby各个路径

-----------------------------------------------------------------------------------------

[embedded@embedded ~]$ whereis ruby
ruby: /usr/bin/ruby /usr/lib/ruby /usr/share/man/man1/ruby.1.gz
[embedded@embedded ~]$ whereis bash
bash: /bin/bash /usr/share/man/man1/bash.1.gz
[embedded@embedded ~]$ whereis python
python: /usr/bin/python /usr/bin/python2.6 /usr/bin/python2.6-config /usr/lib/python2.6 /usr/include/python2.6 /usr/share/man/man1/python.1.gz

-----------------------------------------------------------------------------------------

脚本开头 #!/usr/bin/ruby  类似bash脚本的 #!/bin/bash,包括解释行的命令的路径

# 为注释标记

待续..........................


目录
相关文章
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
Ruby 教程 之 Ruby 方法 2
Ruby return 语句
51 0
|
5月前
|
C语言 C++ Ruby