ruby 語法的總結

2021-04-02 00:17:45 字數 1753 閱讀 7224

沒有函式,用了半天的時間弄環境,用了半天的時間熟悉語法,函式可以自己去查手冊

函式查的網頁在

函式庫

官方站在

編寫工具在 win下使用

def initialize(name,age) #初始化

@name = name;

@age  = age;

puts "i am parent";

end

end

class a                  #直接在類裡新增方法

def to()

return @name;

end

end

#na =a.new('aaa','');    #聲名

#puts na.to_s            #得到類id

#puts na.inspect         #

#puts na.to();

class b < a

def initialize(name='',age='')

super(name,age);        #初始化夫類方法

end

def abc()

puts "i am child!";

end

end

#def na.to()              #過載

#return "456789";

#end

#puts na.to();

#b=b.new();

#b.abc;

#模組使用

module m

def bm

b=b.new();

return "sadf";

end

end

#require "m";

include m;

m.bm();

#模組使用結束

begin

#執行程式

rescue#發生異常

retry#重新執行begin

ensure  #是否發生異常都要執行

end #

#puts m.bm();

#b.abc();

#include m;

#object;

#b.abc();

i=30;

case i;

when 0...5

print '1';

when 6...20

print '2';

when 11...20

print '3';

end

=begin   大段注釋

retry   重新回到迴圈開始

yield   代表程式塊呼叫

while (i<19)

if i==3

puts('y');

#end

#unless i==3 #為false 執行

#puts('n');

#end

#i=i+1;

end

=end

exit(1);

until i<19 #為false 執行

puts i;

i=i+1;

end

ruby的語法問題

這是rury取模運算子的處理方法。ruby中變數沒有類,值才有類。ruby沒有變數宣告的概念。將變數初始化為nil是個好習慣,這當然不會給變數指定型別,也不是真正的初始化,但將告訴解析器這是變數名而不是方法名。解析器如何確定識別符號是變數還是方法?規則是 如果解析器看到識別符號在使用前被賦值,就認為...

Ruby語法了解

三 流程控制 1 條件控制 if 條件表示式 operation1 elsif operation2 else operation3 end unless 條件表示式 operation2 else operation1 end 與if流程相反 2 分支控制 case 表示式 when 值1,值2 ...

Ruby語法小記

0 基本語法 1 輸出 2 輸入 3 基本型別 ruby是沒有型別的,賦值成什麼就是什麼型別。比如 subtotal 100.00,taxrate 0.15,sum 100,name fread 4 全域性變數 在變數前加符號 表示全域性變數。5 類 成員變數 類方法 類例項 object clas...