Instance Variables in ruby

时间:2021-08-02 10:04:48

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 &amp&semi; 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. 安装完Pydev却无法创建Python工程

    为了方便以后工作,今天在ADT里面安装了Pydev(http://pydev.org/updates),可是安装完之后,新建项目的时候却找不到Pydev,perference中也没有. 紧接着尝试安装 ...

  2. 玩爽了!直接在Chrome里抓取数据

    一个小测试发现可以自动做题,于是想通过脚本的方式看能不能获取相应的题库,刚好可以学习一下JS异步操作.花了一天时间,总算跑顺利了,遇到了不少坑.记录下来分享. 1.JS如何顺序执行 JS有强大的异步操 ...

  3. VIM技巧之去除代码行号并缩进代码

    从网上找源代码时经常会发现代码虽然排版很好,但是前面带着行号,直接复制粘贴得将前面的行号去掉才能编译,而更糟糕的是前面带行号,而代码又没排版,简直是噩梦.在VIM中可以轻易地解决这个问题. 这里将网上 ...

  4. 如何在nopcommerce3&period;3注册页面添加密码强度检查仪?

    我刚刚完成了nopCommerce注册页面的密码强度检查仪,因为我觉得在电子商务交易平台,安全问题是非常重要的.在注册页面有必要添加一个密码强度检测仪,以便通知用户他们的密码是否足够强大.今天,大多数 ...

  5. Android基本知识

         Android是Google公司于2007年发布的基于Linux内核的手机操作系统.应用层主要以java为编程语言,应用层分为两层,函数层(Library) 和虚拟机(Virtual).中间 ...

  6. C&num;事件解析

    事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂.而这些东西却往往又是编程中常用且非常重要的东西.大家都知道windows消息处理机制的重要,其实C#事件就是基于window ...

  7. 笔记整理——Linux下C语言正则表达式

    Linux下C语言正则表达式使用详解 - Google Chrome (2013/5/2 16:40:37) Linux下C语言正则表达式使用详解 2012年6月6日Neal627 views发表评论 ...

  8. &period;NET Core:多样的宿主

     .NET Core 可以以以下方式作为宿主运行: IIS 控制台 Windows服务 运行启动代码:         public static void Main(string[] args)   ...

  9. 使用腾讯地图和js&comma;html实现地理位置的获取

    转自http://blog.csdn.net/eele_one/article/details/64905218<!DOCTYPE html> <html> <head& ...

  10. GUI界面操作-实现简单的记事本

    wxPython编写界面程序的基本流程: 1.import wx   #导入wxPython的包 2.class App(wx.App)   #子类化一个应用程序类 3.def onInit(self ...