jtahstu的博客

Git仓库   英文博客  

最新碎语:以后没事写写小的知识点吧

您的位置:jtahstu的博客 >笔记> Ruby小记

Ruby小记

1、类的定义和继承

class Man
  def initialize(name, age)
    @name=name
    @age=age
  end
  def sayname
    puts @name
  end
  def sayage
    puts @age
  end
end
m=Man.new("jtahstu", 21)
m.sayname
m.sayage
b=Man.new("jintao", 20)
b.sayname
b.sayage
class Man2< Man #继承
  def love
    puts "i love you"
  end
end
t=Man2.new("jtahstu", 21)
t.sayname
t.sayage
t.love
D:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) J:/Ruby/Ruby/Test/classTest.rb
jtahstu
21
jintao
20
jtahstu
21
i love you
Process finished with exit code 0

2、异常处理

begin
  @a=1/0
  # raise "this is a yc" 抛出异常
rescue
  puts $!
ensure
  puts "finished"
end
D:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) J:/Ruby/Ruby/Test/raise.rb
divided by 0
finished
Process finished with exit code 0

3、带参数的块

def test_block
  puts "Start"
  yield(1, 2)
  yield(3, 4)
  puts "End"
end
test_block { |a, b| puts a+b }
D:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) J:/Ruby/Ruby/Test/yield.rb
Start
3
7
End
Process finished with exit code 0

4、迭代

# 单行循环块
# 重复三次
3.times { puts "jtahstu" }
puts "---------------"
# 从1循环到9
1.upto(9) { |i| print i," " if i<6 }
puts "\n---------------"
1.upto(9) do |x|
  print x, " "
end
puts "\n---------------"
# 从9到1的循环
9.downto(1) { |i| print i," " if i<6 }
puts "\n---------------"
# 一个从0到12,步长为3的循环
0.step(12, 3) { |i| print i, " " }
puts "\n---------------"
# 取出区间中的元素
(1..9).each { |i| print i, " " if i<6 }
puts "\n---------------"
[1, 1, 2, 3, 5].each { |val| print val, " " }
D:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) J:/Ruby/Ruby/Test/diedai.rb
jtahstu
jtahstu
jtahstu
---------------
1 2 3 4 5 
---------------
1 2 3 4 5 6 7 8 9 
---------------
5 4 3 2 1 
---------------
0 3 6 9 12 
---------------
1 2 3 4 5 
---------------
1 1 2 3 5 
Process finished with exit code 0

5、数字和字符串操作(部分)

i=23;
f=-33.53
puts "i:", i
puts "f:", f
puts "i.to_f", i.to_f
puts "f.to_i:", f.to_i
puts "f.ceil", f.ceil
puts "f.floor", f.floor
puts "f.round", f.round #向上取整
p Math::PI
p Math::E
a="my age : #{11+11}"
b="line NO.#$."
puts a
puts b
a="jtahstu"
a=a*3
puts a
p "aa".succ
p "Az".succ
p "-9".succ
a="jtahstu"
p a["jt"] #包含str则生成一致的字符串
p a["tt"]
a="jtahstu"
p a[2..4]
p a[2...4]
#若干格式化方法
a="jtahstu"
p a.center(20)
p a.ljust(20)
p a.rjust(20)
p a.center(20,"*%")
p a.ljust(20,"*%")
p a.rjust(20,"*%")
p a.count('t')  #t出现的次数
p "123456789".delete("2-8","^4-6")
p "123456789".delete("2378")
str="foobaz"
p str.insert(3,"bar")
p "a".empty?
p "".empty?
p "i love you".include?("love")
p "i love you".split(" ")
D:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) J:/Ruby/Ruby/Test/number.rb
i:
23
f:
-33.53
i.to_f
23.0
f.to_i:
-33
f.ceil
-33
f.floor
-34
f.round
-34
3.141592653589793
2.718281828459045
my age : 22
line NO.0
jtahstujtahstujtahstu
"ab"
"Ba"
"-10"
"jt"
nil
"ahs"
"ah"
"      jtahstu       "
"jtahstu             "
"             jtahstu"
"*%*%*%jtahstu*%*%*%*"
"jtahstu*%*%*%*%*%*%*"
"*%*%*%*%*%*%*jtahstu"
2
"14569"
"14569"
"foobarbaz"
false
true
true
["i", "love", "you"]
Process finished with exit code 0

---

本文章采用 知识共享署名2.5中国大陆许可协议 进行许可,欢迎转载,演绎或用于商业目的。

---

二维码加载中...

扫一扫移动端访问O(∩_∩)O

发表评论

66 + 13 =
路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交
正在加载中……