Ruby学习笔记

#!/usr/bin/ruby

puts "Hello, Ruby, what is your name?"
$name = STDIN.gets
puts "Hi, I am "+$name

def welcome(name,a = 9,b = 9,*c)
    puts "Hi #{name}"
    c.each{|x| print "#{x}"}
#    print "a = "+ $a
    a = 10; b = 11
#    print "Hi, b" + $b
    res = a * b
end

#welcome("RUBY")
puts welcome "ruby",3,6,7,8

x = 100000000000000000000
puts "#{x}  " + x.class.to_s

puts "Hello".upcase()

s = "shit"; f = "fuck"
s,f = f,s
arr = [1, 2, s, f]
puts arr[2]
puts arr[-1]
puts arr.inspect

#Hash Table
cars = {
    'altima' => 'Nissan',
    'camry' => 'Toyota',
    'rx7' => 'Mazda'
}
cars['v'] = 'Volkswagon'
puts cars['rx7']
p cars['v']
#puts *.inspect 缩写
p cars

#each 迭代
toppings = Hash["pancakes","syrup","Pizza","Pepper","Cereal","Sugar"]  
toppings.each{|name, price| puts "#{name} points to #{price}"}
myToppings = toppings.select{|name, price| price > "Pepper"}
p myToppings.inspect

#using of ..[] and ...[)
('a'..'g').each{ |letter| print letter }
puts
('a'...'g').each{ |letter| print letter }
puts

  另存为*.rb文件,在终端执行ruby *.rb。

posted on 2016-06-10 12:44  _bob  阅读(325)  评论(0编辑  收藏  举报