Instance Variables in ruby
Dogs have many shared characteristics, like the abilities to wag their tails and drink water from a bowl, but they also have information about them that is variable,
like their breed or their name.
Similarly, when designing an application, your users will have common traits, like the ability to log in and out, but some information will be variable,
like a username or email address. Let’s start by setting up a new User class. You can follow along in either irb or by running a script from the command line.
class User
# stuff will go here
end
By the end of this lesson, you’ll be able to define a User class that can track its own user information.
Say you wanted to assign a user’s username. You might try something like this.
julia = User.new
julia.username = "coolgirl2000"
# NoMethodError: undefined method `username' for #<User:0x007fc6fa034148>
Let’s pause to take a look at that error bit by bit.
NoMethodError: If I had to guess, I’d say this likely has something to do with a method not existing.undefined method 'username': Suspicions confirmed. It looks like our code can’t find a 'username' method. That makes sense, we never defined one.
But you weren't trying to create a new method! You were thinking about this username like a variable, right?
Ruby has no way of distinguishing between variables and methods in this case, since they both come after the ., so it only supports instance methods.
Instance Variables in ruby的更多相关文章
- Class and Instance Variables In Ruby
https://github.com/unixc3t/mydoc/blob/master/blog/caiv.md
- instance variables may not be placed in categories
Avoid Properties in Categories Objective-C分类中是不允许增加成员变量的(Instance variables may not be placed in cat ...
- swift的属性与变量- Stored Properties and Instance Variables
是一个概念 Stored Properties and Instance Variables If you have experience with Objective-C, you may know ...
- Class Methods & Variables
When calling an instance method like withdraw_securely, the syntax generally looks something like th ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- ruby面向对象class
ruby对象是严格封装的:只能通过定义的方法访问其内部状态.方法使用的成员变量在对象外部不能直接访问,不过可以通过getter.setter等访问器方法(accessor),使他们看起来好像是直接访问 ...
- 《ruby编程语言》笔记 1
赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...
- Ruby学习笔记(二)
1.block 代码块 do...end 或 {} 构成一个代码块,就像常见的 .each后面跟的代码块. my_nums = [1,2,3] my_double_nums = my_nums.col ...
- Day02 - Ruby比一比:Module模块与Class类别
前情提要 在第一天里,我很激昂地用Ruby的类别.物件.方法,写了宣言! class TingIsIronman def initialize @message =“I'm going to writ ...
随机推荐
- VS2008+GDI实现多幅图像的GIF动画制作
相信很多朋友和我一样,经常由于这或那的原因,需制作一些特定格式的图像.如开发过程中需要给菜单.工具条及按钮等添加对应的图形标识,通过代码或资源导入方式加载这些图像时往往会有较高的格式要求. 比如,为按 ...
- 【Aaronyang原创】用linq取出一个集合中重复的数据
文章已经迁移:http://www.ayjs.net/2013/07/69/ 文章已经迁移:http://www.ayjs.net/2013/07/69/ 文章已经迁移:http://www.ayjs ...
- GCD 深入理解:第一部分
虽然 GCD 已经出现过一段时间了,但不是每个人都明了其主要内容.这是可以理解的:并发一直很棘手,而 GCD 是基于 C 的 API ,它们就像一组尖锐的棱角戳进 Objective-C 的平滑世界. ...
- Daily Scrum – 1/19
Meeting Minutes 绑定了快捷键: 改良了user course: 修了一系列Bug: 准备进行演示 Progress part 组员 今日工作 Time (h) 明日计划 Time ...
- Xamarin.Forms——尺寸大小(五 Dealing with sizes)
如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...
- html5_canvas-记忆力卡片游戏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Jquery-获取父级元素parent
1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div"><a id="href_fir" hre ...
- Java设计模式-备忘录模式(Memento)
主要目的是保存一个对象的某个状态,以便在适当的时候恢复对象,个人觉得叫备份模式更形象些,通俗的讲下:假设有原始类A,A中有各种属性,A可以决定需要备份的属性,备忘录类B是用来存储A的一些内部状态,类C ...
- sql-select
SELECT 将资料从数据库中的表格内选出 指令 SELECT "栏位名" FROM "表格名"; 例如:查询Store_Information表中所有的的St ...
- Spring 事物机制
Spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的. 其本质是对方法前后进行拦截,然后在目标方法开始之前创建或 ...