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. 仿照easy-ui并改进的表单验证

    概述 easy-ui有自身的一套表单验证,扩展方便,但默认下也存在一些弱点,比如多规则验证.后台验证.远程异步验证等,这些功能要解决起来是比较吃力的.我仿照它的样式,写了一套前端表单验证的validB ...

  2. Daily English

  3. 【web必知必会】—— 图解HTTP(下)

    上一篇<图解HTTP 上>总结了HTTP的报文格式,发送方式,以及HTTP的一些使用. 本文再总结以下内容: 1 http状态码 2 http报文首部中的各字段 3 http中的身份验证 ...

  4. struts2中怎么把action中的值传递到jsp页面

    对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种: 使用转发视图利用request域中储存所需的值 使用重定向时存储数据进入session使其在jsp中可以获得 下面,让 ...

  5. 可视化HTML编辑器

    [荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛 ...

  6. Json转换为对象

    JObject paramsList = JObject.Parse(OOOO); var obj = paramsList["AAAA"];

  7. NOIP2005提高组 过河

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  8. TCP/IP Four Layer Protocol Format Learning

    相关学习资料 tcp-ip详解卷1:协议.pdf 目录 . 引言 . 应用层 . 传输层 . 网络层 0. 引言 协议中的网络字节序问题 在学习协议格式之前,有一点必须明白,否则我们在观察抓包数据的时 ...

  9. JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    建立了一个Javaweb工程,并在eclipse中配置了Web容器Tomcat.新建的jsp页面,添加一个简单的Java类.可是,JSP页面顶端出现“红色”的报错信息:The superclass & ...

  10. 导师互选系统 Alpha版冲刺总结

    导师互选系统 Alpha版冲刺总结 一.设想和目标 我们的软件什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要是要实现导师和学生双向互选的功能.功能定义清晰明确,在软 ...