码迷,mamicode.com
首页 > 其他好文 > 详细

【夯实Ruby基础】Ruby快速入门

时间:2017-08-26 11:38:24      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:install   ini   ima   代码   系统   src   href   target   apt   

本文地址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

 

【夯实Ruby基础】Ruby快速入门

标签:install   ini   ima   代码   系统   src   href   target   apt   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!