本文地址http://www.cnblogs.com/aiweixiao/p/6664301.html

文档提纲

 

扫描关注微信公众号 

1.Ruby安装

  1.1)【安装Ruby】

    Linux/Unix 上的 Ruby 安装

    Windows 上的 Ruby 安装

$  sudo yum install ruby    # CentOS, Fedora, 或 RHEL 系统
或
sudo apt-get install ruby-full # Debian 或 Ubuntu 系统
$ brew install ruby  #苹果系统

 

  1.2)【交互式 Ruby(IRb)】

    命令提示符中键入 irb,一个交互式 Ruby Session 将会开始

2.Ruby语法

  2.1)【字符串】

    -- 【输出】

1 puts "Hello, world!"

    --【中文编码】

      中文会出现乱码,解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*-(EMAC写法) 或者 #coding=utf-8 就行了

#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-

puts "你好,世界!";

  2.2)【类】

class Customer
end

    -- ruby的类,必须先定义,才能使用

  --代码示例

#!/usr/bin/ruby
 
class Customer
   @@no_of_customers=0
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
    def total_no_of_customers()
       @@no_of_customers += 1
       puts "Total number of customers: #@@no_of_customers"
    end
end

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!