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的更多相关文章

  1. Class and Instance Variables In Ruby

    https://github.com/unixc3t/mydoc/blob/master/blog/caiv.md

  2. instance variables may not be placed in categories

    Avoid Properties in Categories Objective-C分类中是不允许增加成员变量的(Instance variables may not be placed in cat ...

  3. swift的属性与变量- Stored Properties and Instance Variables

    是一个概念 Stored Properties and Instance Variables If you have experience with Objective-C, you may know ...

  4. Class Methods & Variables

    When calling an instance method like withdraw_securely, the syntax generally looks something like th ...

  5. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  6. ruby面向对象class

    ruby对象是严格封装的:只能通过定义的方法访问其内部状态.方法使用的成员变量在对象外部不能直接访问,不过可以通过getter.setter等访问器方法(accessor),使他们看起来好像是直接访问 ...

  7. 《ruby编程语言》笔记 1

    赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...

  8. Ruby学习笔记(二)

    1.block 代码块 do...end 或 {} 构成一个代码块,就像常见的 .each后面跟的代码块. my_nums = [1,2,3] my_double_nums = my_nums.col ...

  9. Day02 - Ruby比一比:Module模块与Class类别

    前情提要 在第一天里,我很激昂地用Ruby的类别.物件.方法,写了宣言! class TingIsIronman def initialize @message =“I'm going to writ ...

随机推荐

  1. 汇顶科技&&硬件类笔试题目

    汇顶科技硬件类笔试题目,每年都有变化,但是题目类型都差不多.汇顶科技17年在南京地区大概招了20个左右吧,给的待遇还是不错的,工作地点上海深圳

  2. hello,world!

    这可以算是我的第一篇博客了吧?真正意义上的开始... 我于2016年6月毕业于山东英才学院信工学院计算机管理系,至今已有近半年.几经周转,总算是准备稳定下来了.于是,当初的博客想法提上了日程. 本人工 ...

  3. [Aaronyang] 写给自己的WPF4.5 失传的第十本秘籍4[wpf使用FontAwesome,并送其他3招心法]

    总有一个人他教会你成长,然后又独自离开--Aaronyang的博客(www.ayjs.net)-www.8mi.me =============时隔两年后再看WPF,有些秘籍不太适合公开,公开了就不值 ...

  4. 百度地图整合功能分享修正版[ZMap.js] 实例源码!

    ZMap 功能说明 ZMap 是学习百度地图 api 接口,开发基本功能后整的一个脚本类,本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐标是 ...

  5. Beta项目冲刺汇总贴

    第一天 http://www.cnblogs.com/Allenbi/p/5003704.html 第二天 http://www.cnblogs.com/Allenbi/p/5020820.html ...

  6. 使用github托管代码心

    这次使用github托管代码并没有下载客户端git for windows,而是使用eclipse里面自带的git上传了hello world这个项目,步骤如下: 1.首先创建项目:file-> ...

  7. ORA-12737: Instant Client Light: unsupported server character set CHS16GBK/ZHS16GBK解决方案

    二.Navicat for Oracle的配置 1.启动该工具,出现如下的开始界面,单击“连接”选项,进行连接数据库,如图所示: 6.在“新建连接”对话框中,输入任意的连接名,选择默认的连接类型,输入 ...

  8. poj1679 次小生成树

    prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...

  9. Java基础-final和static的区别

    很多时候会容易把static和final关键字混淆,static作用于成员变量用来表示只保存一份副本,而final的作用是用来保证变量不可变.看下面这个例子: public class Test { ...

  10. 【POJ 2485】Highways(Prim最小生成树)

    题目 Prim算法:任选一个点,加入集合,找出和它最近的点,加入集合,然后用加入集合的点去更新其它点的最近距离......这题求最小生成树最大的边,于是每次更新一下最大边. #include < ...