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 ...
随机推荐
- 推荐一个 angular 图像加载插件
推荐一个简单的 Angular 图片加载插件:vgSrc,插件根据图片资源的不同加载状态,显示不同图片,亲测兼容IE-8. 使用 推荐使用 bower 加载: bash bower install v ...
- .net 开发人员如何自处
关于N家还是J家,谁家更阔绰有前途的问题,每年都要讨论一会,当然和各位兄弟在岗位上迷茫,不知位置在哪有关系,不过这个问题基本算是伪问题,这种东西放到更高的维度,真的什么都不是. 但是为什么J家好像是不 ...
- “未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
产生原因: 使用nuget管理程序包,同一个解决方案里面有不同版本的Entity Framework,有可能在不同时间安装不同版本的Entity Framework,所以出现这个问题. 解决方案: 1 ...
- jQuery基础之(六)jQuery浏览器的兼容性(以CSS3特写兼容举例)
CSS3充分吸收多年了web发展的需求,吸收了很多新颖的特性.例如border-radius css3特性中最令人兴奋的莫过于选择器的增强,例如属性选择器可以根据某个属性值来选择标记,位置选择器可以根 ...
- java输入函数
import java.util.*; class Test{ public static void main(String[] args){ Scanner x=new Scanner(System ...
- Javascript写入txt和读取txt文件示例
1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Var fso=new ActiveXObject(Scripting.FileSystem ...
- python 元祖(tuple)
元祖和列表几乎相同,但是元祖一旦初始化是不可变更内容的 元祖的表示方式是caassmates=(), 要记住所有列表能用的.元祖都能用,但是就是不能变内容 注:记住,在python中的元祖,为了引起不 ...
- Jquery-处理iframe的高度自适应
超级简单的方法,也不用写什么判断浏览器高度.宽度啥的.下面的两种方法自选其一就行了.一个是放在和iframe同页面的,一个是放在test.html页面的.注意别放错地方了哦. iframe代码,注意要 ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- 【poj1012】 Joseph
http://poj.org/problem?id=1012 (题目链接) 半年前的考试题..任然清晰的记得那次差10分就AK... 题意 约瑟夫环,有前k个好人,后k个坏人,要求使得后k个坏人先死的 ...