Ruby: 学习日志1: 基础

本文介绍了Ruby编程语言的基础知识,包括环境搭建、基本语法如变量、数组、哈希等数据类型的操作,以及条件语句、循环语句的使用。还演示了如何使用块(block)和迭代器进行高效编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 郁闷坏了, 写了三个小时的ruby笔记居然在提交时丢失了.

只好重新写一点儿还想得起的.

ruby-doc
http://www.ruby-doc.org/


Pragmatic Programmer ruby  study source code
http://www.pragmaticprogrammer.com/titles/ruby/code/

ruby source code
http://www.ruby-lang.org/en/downloads/

One-Click Ruby Installer for Windows

http://rubyinstaller.rubyforge.org/

函数
def say_goodnight(name)
    result = "good night " + name
    return result
end
#time for bed...
puts say_goodnight("olojiang")
puts say_goodnight("yy")

运行

ruby filename

ruby
    puts "Hello World"
Ctrl + D

ri
ri -c
ri GC
ri GC::start
ri GC.start
ri --help

irb
irb(main):001:0> "asdfl;kjsdfsdf".length
=> 14
irb(main):002:0> "risk".index("c")
=> nil
irb(main):003:0> "risk".index("s")
=> 2
irb(main):004:0> -192.abs
=> 192
irb(main):005:0> exit

#function
def say_goodnight(name)
    result = "good night " + name
    return result
end

def say_goodnight2(name)
    "good night, #{name.capitalize}"
end
#call method
puts
puts "************* call method"
puts say_goodnight("olojiang")
puts say_goodnight("yy")

puts(say_goodnight("jw"))

puts("And good night!/nGrandma")

#in a String #{variable}
puts
puts "************* in a String #"
puts(say_goodnight2("yjl"))
puts(say_goodnight2("jw"))

#variable
puts
puts "************* variable"
$greeting = "Hello"
@name = "Procedure"
puts("#$greeting #@name")

#array
puts
puts "************* array"
a = [1, "test String", 234.3]
puts a[2]
a[0] = nil
puts a
a = ['ant', 'cat']
puts a
a = %w[x y z]
puts a

#hash
puts
puts "************* hash"
hash_table = {
    "key1" => "value1",
    "key2" => 2,
    3 => [234,33.3]
}
puts hash_table["key1"]
puts hash_table["key2"]
puts hash_table[3][1]

hash_table2 = Hash.new(999)
puts("default hash value: #{hash_table2["X"]}")

#if
puts
puts "************* if"
count = 10
if count>0
    count -=5
    puts count
end

count -= 10 if count>= 5
puts count

#while
puts
puts "************* while"
count = 11
while(count>0)
    count -=5
    puts count
end

num = 2
num = num*num while num<1000
puts(num)


#regular expression
puts
puts "************* regular expression"
puts "hasdfsdf" =~ /f./
puts "hasdfsdf" =~ /df.*f/
line = "Perl is good, Python is nice"
puts(line.sub(/Perl/, "Ruby"))
puts(line.gsub(/Perl|Python/, "Ruby"))

#block
puts
puts "************* block"

def greet
    puts "start method"
    yield("greet")
    yield("greet")
    puts "end method"
end
greet {|method_name| puts "single line block in: #{method_name}" }

puts

greet do
    |method_name|
    puts "multi line"
    puts "block"
    puts "method name is: #{method_name}"
end

#for each and block
puts
puts "************** for each and block"

['cat', 'dog', 'horse'].each{|name| print name, " "}
puts
5.times {print "*"}
puts
3.upto(6) { |i| print i , " "}
puts
('a'..'e').each {|char| print char, " "}
puts

#printf
puts
puts "************** printf"
printf("Number: %5.2f /nString: %s/n", 23.234, "hello")

#gets
puts
puts "************** gets"
print "input: "
line = gets
puts line

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值