#全局变量
$global_variable=10
class Class1
def print_global()
puts "Global variable in Class1 is #{$global_variable}"
end
end
class1obj=Class1.new
class1obj.print_global()
 #实例变量
class Customer
def initialize(id,name,addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_detalis
puts "Customer id #{@cust_id}"
puts "Customer name #{@cust_name}"
puts "Customer addr #{@cust_addr}"
end
end
cust1=Customer.new("","John","Wisdom")
cust1.display_detalis
 #类变量
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 addr #{@cust_addr}"
end
def total_no_of_customers()
@@no_of_customers +=1
puts "Totla number of customers:#{@@no_of_customers}"
end
end
cust1=Customer.new("","John","Wisdom")
cust2=Customer.new("","Poul","New") cust1.total_no_of_customers
cust2.total_no_of_customers

ruby学习--varaible的更多相关文章

  1. ruby 学习笔记 1

    写ruby blog  系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...

  2. Ruby学习心得之 Linux下搭建Ruby环境

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Ruby学习心得之 Linux下搭建Ruby环境1.前言2.Linux下安装Ruby环境 一 ...

  3. Ruby学习之mixin

    直接上代码: module Action def jump @distance = rand(4) + 2 puts "I jumped forward #{@distance} feet! ...

  4. ruby学习网站

    Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...

  5. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  6. ruby学习笔记(1)-puts,p,print的区别

    ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...

  7. Ruby学习资源汇总

    from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...

  8. Ruby学习之深入类

    在讨论对象模型时,对类做了初步了解,关于类本身,还有许多知识需要学习. 类定义 Ruby中,可以用class关键字或者Class.new方法来定义一个类,在Ruby中,类定义的同时就是在运行代码,类和 ...

  9. Ruby学习之代码块

    代码块在其他的语言中都或多或少接触过一些,如perl中sort{$a<=>$b}keys,传入代码块实现按数值排序,在swift中用到闭包,更加深入学习到training closure. ...

随机推荐

  1. PHP抓取豆瓣读书爬虫代码

    <?php//演示地址 http://asizu.sinaapp.com/reptile_douban.php//数据量不是特别大,没有写抓完数据便停止. 喜欢的朋友拿去自己改改就好了 head ...

  2. 在VS2012中使用GDI+

    首先说明,在VS的较高版本中,已经包含GDI+的SDK,不用再次下载,只需要使用前应用相应的头文件,添加些代码即可.但是VC6.0中,没有GDI+SDK,需要同志们下载才行. 步骤: 1.在stdaf ...

  3. C# 绘制统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  4. jQuery关于文字内容溢出用点点点(…)省略号表示

    1.jQuery限制字符字数的方法代码很简单,使用也很方便,如下: $(document).ready(function(){//限制字符个数$(“.zxx_text_overflow”).each( ...

  5. SCCM2012分发脚本

    1.分发批处理脚本 命令行:script.bat 2.分发PowerShell脚本 命令行:PowerShell.exe -executionpolicy unrestricted -file .\s ...

  6. [转]Swift Cheat Sheet

    原文:http://kpbp.github.io/swiftcheatsheet/ A quick cheat sheet and reference guide for Apple's Swift ...

  7. PHP 自动生成导航网址的最佳方法 v20130826

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法. 最终积累了这种最写法,算是最好的吧.附上截图和代码如下(PHP+HTML) <?php $linkArr = array( 'ind ...

  8. hdu 5029 Relief grain(树链剖分+线段树)

    题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...

  9. 了解PHP中Stream(流)的概念与用法(转)

    Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...

  10. [转]使用 PIVOT 和 UNPIVOT

    可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表.PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执 ...