郁闷坏了, 写了三个小时的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/
函数
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
只好重新写一点儿还想得起的.
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