HeadFIrst Ruby 第二章总结 methods and classes

前言

这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instance variables 和 instance methods.和它们需要注意的一些问题.

创建 method 相关

问题1:括号 be or not be?

在 Ruby 中,如果需要创建的 method 包含参数,那么后面应该有“()” ;
如果不需要任何参数,则不需要加“()”,在调用函数的时候也不需要加.

问题2: local variable 和 instance variable 的区别

local variable:在定义的 method 范围之后,就 don't exisit 了
instance variable: 在定义的 class 的范围内都 exisit.

新知1:在 Terminal 中运行程序的另一种方法

特点:可以 load 这个 file 之后,然后进行交互的编程.

步骤:

  1. 在 Terminal 中输入 “irb -I ."
  2. 接着在提示下输入 "require xxx.rb"

新知2: atttribute accessor methods

定义:为了实现 encapsulation,如果想要给创建的 instance 传递参数的话,就需要通过 accessor methods 进行传递,它分为两类: attribute writer 和 attribute reader

attribute writer

定义:用于set an instance variable
惯例: attribute writer 的名字与 instance variable 的名字相同,结尾为 "=".
格式:

def my_attribute=(new_value)
@my_attribute = new_value
end

attribute writer

定义:用于 get the value of an variable back
格式:

def my_attribute
@my_attribute
end

attribute accessor

可以用

  • attr_writer :name
  • attr_reader :name
  • attr_accessor :name

代替 def 格式的代码,它们是 equivalent 的

HeadFIrst Ruby 第二章总结 methods and classes的更多相关文章

  1. HeadFirst Ruby 第九章总结 mixins & modules

    前言 如果想要复用 method, 可用的方法是针对 Class 的 inheritance,但是, inheritance has its limitations,它的缺点有: 只能 inhert ...

  2. 3-8《Ruby元编程》第二章对象模型

    <Ruby元编程> 第二章 对象模型 类定义揭秘inside class definitions: class关键字更像一个作用域操作符,核心作用是可以在里面随时定义方法. [].meth ...

  3. Ruby学习-第二章

    第二章 类继承,属性,类变量 1.如何声明一个子类 class Treasure < Thing 这样Thing类中的属性name,description都被Treasure继承 2.以下三种方 ...

  4. PRML读书会第六章 Kernel Methods(核函数,线性回归的Dual Representations,高斯过程 ,Gaussian Processes)

    主讲人 网络上的尼采 (新浪微博:@Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:16:05 今天的主要内容:Kernel的基本知识,高斯过程.边思考边打字,有点慢, ...

  5. C#本质论读书笔记:第一章 C#概述|第二章 数据类型

    第一章 1.字符串是不可变的:所有string类型的数据,都不可变,也可以说是不可修改的,不能修改变量最初引用的数据,只能对其重新赋值,让其指向内存中的一个新位置. 第二章 2.1 预定义类型或基本类 ...

  6. JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助

    上一篇:JAVA学习之Ecplise IDE 使用技巧(1)第一章:我的地盘我做主,工作空间 第二章:键盘小快手,代码辅助 内容包括: 第一:显示行号 如何设置行号:Ecplice菜单Windows& ...

  7. Knockout应用开发指南 第二章:监控属性(Observables)

    原文:Knockout应用开发指南 第二章:监控属性(Observables) 关于Knockout的3个重要概念(Observables,DependentObservables,Observabl ...

  8. 《DOM Scripting》学习笔记-——第二章 js语法

    <Dom Scripting>学习笔记 第二章 Javascript语法 本章内容: 1.语句. 2.变量和数组. 3.运算符. 4.条件语句和循环语句. 5.函数和对象. 语句(stat ...

  9. 《Node.js 高级编程》简介与第二章笔记

    <Node.js 高级编程> 作者简介 Pedro Teixerra 高产,开源项目程序员 Node 社区活跃成员,Node公司的创始人之一. 10岁开始编程,Visual Basic.C ...

随机推荐

  1. Java 判断字符串是否为空的四种方法、优缺点与注意事项

    以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: ...

  2. Python 用pygame模块播放MP3

    安装pygame(这个是python3,32位的) pip安装这个whl文件 装完就直接跑代码啦,很短的 import time import pygame file=r'C:\Users\chan\ ...

  3. SVM学习笔记4-核函数和离群点的处理

    核函数在svm里,核函数是这样定义的.核函数是一个n*n(样本个数)的矩阵,其中:$K_{ij}=exp(-\frac{||x^{(i)}-x^{(j)}||^{2}}{2\sigma ^{2}})$ ...

  4. CentOS7 安装git服务器

    在CentOS7系统中安装git服务器有两种方法,分别为yum安装和下载git安装包手动安装,这篇文章只有下载git安装包手动安装方法. 方法一:使用yum安装 暂无 方法二:下载git安装包手动安装 ...

  5. .NET BackgroundWorker的一般使用方式

    代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  6. 在fedora23中安装virtualbox, 然后实现虚拟机irtualbox 或者 vmware 下的xp操作系统

    参考: http://blog.csdn.net/statdm/article/details/7756788 参考: http://www.cnblogs.com/fengbohello/p/488 ...

  7. luoguP4072 [SDOI2016]征途

    [SDOI2016]征途 大体 大概就是推推公式,发现很傻逼的\(n^3\)DP get60 进一步我们发现状态不能入手,考虑优化转移 套个斜率优化板子 每一层转移来一次斜率优化 思路 先便便式子 \ ...

  8. ProgrammingError: You must not use 8-bit bytestrings...

    问题出现: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte ...

  9. (zhuan) LSTM Neural Network for Time Series Prediction

    LSTM Neural Network for Time Series Prediction Wed 21st Dec 2016 Neural Networks these days are the ...

  10. website for .Net Core

    5 Ways to Build Routing in ASP.NET Core Bundling in .NET Core MVC Applications with BundlerMinifier. ...