`
xuchunming
  • 浏览: 30224 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ruby学习笔记系列(2)

    博客分类:
  • ruby
阅读更多
class variables & class methods

A class variable is shared among all objects of a class, and it is also accessible to
the class methods that we’ll describe later. Only one copy of a particular class variable
exists for a given class. Class variable names start with two “at” signs, such as @@count.
Unlike global and instance variables, class variables must be initialized before they
are used. Often this initialization is just a simple assignment in the body of the class
definition.
For example, our jukebox may want to record how many times each song has been
played. This count would probably be an instance variable of the Song object. When
a song is played, the value in the instance is incremented. But say we also want to
know how many songs have been played in total. We could do this by searching for all
the Song objects and adding their counts, or we could risk excommunication from the
Church of Good Design and use a global variable. Instead, we’ll use a class variable.
class Song
@@plays = 0
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
@plays = 0
end

def play
@plays += 1 # same as @plays = @plays + 1
@@plays += 1
"This song: #@plays plays. Total #@@plays plays."
end
end
For debugging purposes, we’ve arranged for Song#play to return a string containing
the number of times this song has been played, along with the total number of plays for
all songs.We can test this easily.
s1 = Song.new("Song1", "Artist1", 234) # test songs..
s2 = Song.new("Song2", "Artist2", 345)
s1.play ! "This song: 1 plays. Total 1 plays."
s2.play ! "This song: 1 plays. Total 2 plays."
s1.play ! "This song: 2 plays. Total 3 plays."
s1.play ! "This song: 3 plays. Total 4 plays."
Class variables are private to a class and its instances. If you want to make them accessible
to the outside world, you’ll need to write an accessor method. This method could
be either an instance method or, leading us neatly to the next section, a class method.
Class Methods
Sometimes a class needs to provide methods that work without being tied to any particular
object. We’ve already come across one such method. The new method creates a
new Song object but is not itself associated with a particular song.
song = Song.new(....)
You’ll find class methods sprinkled throughout the Ruby libraries. For example, objects
of class File represent open files in the underlying file system. However, class File
also provides several class methods for manipulating files that aren’t open and therefore
don’t have a File object. If you want to delete a file, you call the class method
File.delete, passing in the name.
File.delete("doomed.txt")
Class methods are distinguished from instance methods by their definition; class methods
are defined by placing the class name and a period in front of the method name (but
also see the sidebar on page 34).
class Example
def instance_method # instance method
end
def Example.class_method # class method
end
end

Jukeboxes charge money for each song played, not by the minute. That makes short
songs more profitable than long ones.We may want to prevent songs that take too long
from being available on the SongList. We could define a class method in SongList
that checked to see if a particular song exceeded the limit. We’ll set this limit using
a class constant, which is simply a constant (remember constants? They start with an
uppercase letter) that is initialized in the class body.
class SongList
MAX_TIME = 5*60 # 5 minutes
def SongList.is_too_long(song)
return song.duration > MAX_TIME
end
end
song1 = Song.new("Bicylops", "Fleck", 260)
SongList.is_too_long(song1) ! false
song2 = Song.new("The Calling", "Santana", 468)
SongList.is_too_long(song2) ! true
分享到:
评论

相关推荐

    个人ruby学习笔记

    从给定的文件信息中,我们可以提炼出一系列关于Ruby编程语言的重要知识点,涵盖基础语法、变量类型、数值操作、条件语句、循环结构以及字符串处理等核心领域。 ### Ruby基础语法 #### 注释 Ruby提供了两种注释方式...

    ruby笔记2ruby笔记2ruby笔记2

    "ruby笔记2ruby笔记2ruby笔记2"可能是指一系列关于Ruby学习的笔记,这些笔记可能涵盖了Ruby的基础概念、核心特性以及进阶话题。在Ruby的学习过程中,理解和掌握以下几个关键知识点至关重要: 1. **面向对象编程...

    ROR 学习笔记系列一 Windows XP下环境安装配置(2)

    在本篇ROR(Ruby on Rails)学习笔记中,我们将深入探讨如何在Windows XP操作系统上进行环境的安装和配置。Ruby on Rails是一个流行的开源Web应用框架,它基于Ruby编程语言,以其“DRY”(Don't Repeat Yourself)...

    Ruby学习资料(含参考手册和Programming Ruby)-中文.rar

    这份"Ruby学习资料(含参考手册和Programming Ruby)-中文.rar"压缩包包含了一系列的资源,帮助初学者和进阶者深入理解Ruby语言。 首先,"ruby中文文档(含参考手册和Programming Ruby).chm"是一个综合性的中文参考...

    Extjs 5 学习笔记

    ### Extjs 5 学习笔记之 SenchaCmd 深入解析 #### 一、SenchaCmd 的简介 SenchaCmd 是一个跨平台的命令行工具,它为基于 ExtJS 和 Sencha Touch 应用程序的开发周期提供了全面的支持。从创建应用程序的基础结构到...

    ruby on rails环境搭建学习笔记;passenger+nginx环境配置

    在进行 Ruby on Rails 的开发之前,首先需要完成一系列的基础软件安装工作,包括但不限于 Ruby 语言环境、Rails 框架、版本管理工具等。 ##### 安装基础依赖 1. **更新系统包列表:** ```sh sudo apt-get update...

    Ruby On Rails开发从头来系列教程(chm)

    摘要:一直想尝试Ruby On Rails,但是因为对apache,mysql都不熟,对Rails的环境搭建更是没信心,所以一直没有开始,从知道了InstantRails后,终于在windows上搭建了Ruby On Rails开发环境,开始了Rails的学习。...

    solr_学习笔记_v1.1

    ### Solr学习笔记_v1.1 - 高性能搜索引擎的核心参数与查询语法详解 #### 一、Query参数:深入理解Solr查询的核心要素 Solr作为高性能的全文搜索引擎,其强大的查询能力很大程度上依赖于一系列精细调整的参数。以下...

    java学习笔记JDK6课件和课本代码

    这个"java学习笔记JDK6课件和课本代码"资源很可能是包含了一系列的教程、PPT课件、示例代码和练习题,帮助初学者或者需要回顾JDK 6特性的开发者深入理解和实践。通过这些资料,你可以学习如何使用JDK 6中的新功能,...

    GDAL-Python库学习笔记

    ### GDAL-Python库学习笔记 #### 一、GDAL 库简介 ##### 1.1 引言 GDAL(Geospatial Data Abstraction Library)是一个强大的库,旨在为栅格地理空间数据提供翻译与处理功能。对于从事地理信息系统(GIS)或遥感...

    GDAL库学习笔记 资料

    ### GDAL库学习笔记知识点概览 #### 一、GDAL库简介 - **定义与功能**:GDAL(Geospatial Data Abstraction Library)是一个用于处理栅格地理空间数据格式的开源库,由开放源代码地理空间基金会(Open Source ...

    Java JDK 6学习笔记——ppt简体版

    Java JDK 6学习笔记主要涵盖了Java开发工具集(Java Development Kit)的第六个主要版本,这是一个重要的里程碑,因为它为开发者提供了许多新特性和改进。在这个版本中,Java平台的稳定性和性能得到了显著提升,同时...

    rubylearningdotorg:Ruby学习课程

    标题"rubylearningdotorg:Ruby学习课程"表明这是一个关于学习Ruby编程语言的资源,可能包括一系列的教程、笔记或实践活动,适合初学者和有一定基础的开发者。"肯·麦卡登"可能是课程的创建者或主要贡献者,他可能是...

    calabash-android学习笔记V0.2.docx

    1. **安装Ruby**:确保已安装Ruby,并通过在终端中运行`ruby -v`检查版本,应显示“ruby 1.8.7”或更高版本。对于Windows用户,可以从RubyInstaller.org获取Ruby。 2. **安装Android SDK**:需要安装Android SDK,...

    GDAL库学习笔记.pdf

    该库由开源地理空间基金会(OSGeo)提供,采用X/MIT开源许可证,支持多种编程语言,如C/C++、Python、Ruby、VB、Java、C#等。GDAL被广泛应用于地理信息系统(GIS)和遥感(RS)领域。 GDAL安装: 1. GDAL的安装过程...

    GDAL学习笔记.doc

    GDAL不仅提供了一个统一的数据模型来处理多种格式的栅格数据,还附带了一系列命令行工具,用于数据转换和处理。 GDAL的核心功能在于其强大的数据格式支持,能够读取和写入众多的栅格数据格式,包括但不限于JPG、GIF...

    JSON学习笔记

    这篇"JSON学习笔记"可能涵盖了以下几个关键知识点: 1. JSON的基本结构:JSON数据由键值对(key-value pairs)组成,键和值之间用冒号隔开,键值对之间用逗号分隔。键必须是字符串,而值可以是字符串、数字、布尔值...

Global site tag (gtag.js) - Google Analytics