最近在看《七周七语言》,适当了解一些C语言家族以外的编程语言也很有意思,下面记录一下各个语言的语法特点。
一,irb打开Ruby的交互命令行
> language = 'ruby' => "ruby" > puts 'hello, #{language}' hello, #{language} #单引号之间变量不被替换 > puts "hello, #{language}" hello, ruby
二,条件判断可以放在语句后面
> puts 'This appears to be true.' if language == 'ruby' This appears to be true. => nil > puts 'This appears to be true.' unless language == 'ruby' => nil > x += 1 while x < 10 > x -= 1 until x == 1
三,数组/散列表
> arr = [] => [] > arr[1] = 3 => 3 > arr[2] = {"name"=>"A","hobby"=>"B"} => {"name"=>"A", "hobby"=>"B"} > arr => [nil, 3, {"name"=>"A", "hobby"=>"B"}] #第一个元素为nil #数组简单操作如下 > arr.each {|a| puts a} #代码块方式遍历数组 3 {"name"=>"A", "hobby"=>"B"} => [nil, 3, {"name"=>"A", "hobby"=>"B"}] > a = [5,3,2,4,1] => [5, 3, 2, 4, 1] > a.sort => [1, 2, 3, 4, 5] > a.any? {|i| i > 6} => false > a.any? {|i| i > 4} => true > a.all? {|i| i > 4} => false > a.collect {|i| i * 2} => [10, 6, 4, 8, 2] > a.select {|i| i % 2 == 0} => [2, 4] > a.max => 5 > a.member?(3) => true > a << 6 => [5, 3, 2, 4, 1, 6] #散列有如下两种使用方法 > hash_table1 = {'name' => 'ciaos', 'age' => 3} => {"name"=>"ciaos", "age"=>3} > hash_table1["name"] => "ciaos" > hash_table2 = {:name => "ciaos", :age => 3} => {:name=>"ciaos", :age=>3} > hash_table2[:name] => "ciaos" #散列作为函数参数传递 > def test(para = {}) > puts para > end > test {} => nil > test :para1 => "OK", :para2=> :Right, "para3" => nil {:para1=>"OK", :para2=>:Right, "para3"=>nil} => nil
四,类简单使用方法
> class Person > attr_accessor :name, :age > def initialize(name, age) > @name = name > @age = age > end > def say() > puts name + " " + age.to_s > end > end > p = Person.new("ciaos",25) => #<Person:0x00000000c76aa0 @name="ciaos", @age=25> > p.say ciaos 25 => nil
五,其他知识点
1,method_missing类似php的__call函数,当找不到方法时会出发此函数
2,通过继承或者编写模块拓展功能
> class Car > ... > end > class Toyota < Car > end > module Tool > ... > end > class Toyota > include Tool > end
总结:
1,不错的脚本语言,结合rails适合web开发
2,并发上性能存在瓶颈