Attribute is a very useful propety in Ruby.It like the 'get/set' method in Java and the 'property' in C#,but it is easier and more useful than these language.For example,in Java language you need write too codes like these:
class A{
private int a;
public int getA(){
return a;
}
public void setA(int a){
this.a = a;
}
....
}
And you see,there are too many codes.In C#,it may be easyer than java,but you need write more code,too.
In Ruby,you can write 'get method' codes like the following:
attr_reader :name
then,if there are many instance variables which need be read,you can write following:
attr_reader :name,:artist,:duration
Use ',' to separate these variables(Note:remember that there is a space between the 'reader' word and the ':').
The 'set' method is like
attr_writer :name,:artist,:duration
And the 'get/set' method:
attr_accessor :name,:artist,:duration
Now,you can see why I say that the Attribute in Ruby is easier.And you will know why I say that the Attribute in Ruby is more useful in the next part which I reference from
<pragmatic.programmers.programming.ruby.2nd>:
These attribute-accessing methods do not have to be just simple wrappers around an
object¡¯s instance variables. For example, you may want to access the duration in minutes
and fractions of a minute, rather than in seconds as we¡¯ve been doing.
</pragmatic.programmers.programming.ruby.2nd>
ruby 代码
- class Song
- def duration_in_minutes
- @duration/60.0
- end
- def duration_in_minutes=(new_duration)
- @duration = (new_duration*60).to_i
- end
- end
- song = Song.new("Bicylops", "Fleck", 260)
- song.duration_in_minutes ! 4.33333333333333
- song.duration_in_minutes = 4.2
- song.duration ! 252
Here we¡¯ve used attribute methods to create a virtual instance variable. To the outside
world, duration_in_minutes seems to be an attribute like any other. Internally,
though, it has no corresponding instance variable.
This is more than a curiosity. In his landmark book Object-Oriented Software Construction
[Mey97], Bertrand Meyer calls this the Uniform Access Principle. By hiding
the difference between instance variables and calculated values, you are shielding the
rest of the world from the implementation of your class. You¡¯re free to change how
things work in the future without impacting the millions of lines of code that use your
class. This is a big win.
Note:there is a '=' when you define the
Virtual Attribute between the name of attribute and the argument
ruby 代码
- def duration_in_minutes=(new_duration)
分享到:
相关推荐
Ensure that you activate it for RFC in the attributes tab (in SE37). #### Structure Definition Define a new structure called `ZBAPIUSNAME` and include the two structures `BAPIUSNAME` and `USLOCK` ...
2. **属性**(Attributes):如`@gregorian_date`,用于存储阳历日期,以及可能的`@lunar_date`用于存储农历日期。 3. **转换方法**:为了将阳历转换为农历,类可能包含一个名为`to_lunar`的方法,它会根据特定算法...
Ruby的控制流程语句包括条件判断(if/else/elsif)、循环(for/in, while, until)以及开关语句(case/when)。例如: ```ruby if condition # do something else # do something else end ``` 循环语句如: ```...
Ruby官方曾在2008年8月发布了一则安全公告([http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/](http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/)),指出REXML...
YTToast.showToast(withText: "Custom attributes", duration: 2.0, position: .center, attributes: attributes) { _ in } ``` 6. 显示带有图片的Toast: 如果需要在Toast中包含图片,可以使用`showToastWithText:...
Rails for Zombies教程中的"Deep in the CRUD"部分深入讲解了CRUD(创建Create、读取Retrieve、更新***e、删除Delete)操作。CRUD是数据库管理系统中应用最广泛的四个基本操作,用于管理数据库中数据的生命周期。 ...
模型通常包含属性(attributes)和方法(methods),如 `create`, `update`, `destroy` 等操作。 2. **View**(视图):是用户看到和与之交互的部分。在 `rBlog` 中,视图文件(通常为 `.erb` 扩展名)用于渲染 ...
- **NIO.2 (New I/O API):** Enhancements to the non-blocking I/O capabilities, including new classes for file system operations, file channels, and file attributes. - **Scripting Language Support:** ...
类是创建对象的蓝图,它定义了对象的属性(attributes)和行为(methods)。在Ruby中,你可以使用`class`关键字来定义一个类,并通过`end`关键字结束。例如: ```ruby class Person attr_accessor :name, :age ...
RSpec 是一种用于 Ruby 编程语言的行为驱动开发(Behavior-Driven Development, BDD)框架。它提供了一种易于理解的方式来编写测试代码,使开发者能够更加清晰地表达出他们期望软件如何行为。 #### 二、RSpec 的...
page.addText(text, with: attributes, in: textRect) if let imageData = pdf.generateImage() { // 使用生成的UIImage } ``` 在这个例子中,我们创建了一个PDFGenerator实例,添加了一个新的PDF页面,并设置了...
挑战冠军 部署地点 安装 此应用程序使您可以挑战朋友并将他们变成敌人! 要启动并运行该应用程序,请在控制台中运行以下命令: bundle install rake db:create:all ... Add all of the tables and table attributes
NSDictionary *attributes = element.attributes; // 处理提取到的数据... } ``` 其次,"更新了下拉刷新功能"意味着这个项目集成了iOS的下拉刷新(Pull-to-Refresh)特性。在iOS中,最常用的下拉刷新组件是`...
[2014-01-24T16:15:55+00:00] WARN: Previous package[perl]: /tmp/vagrant-chef-1/chef-solo-1/cookbooks/perl/recipes/default.rb:26:in `block in from_file' [2014-01-24T16:15:55+00:00] WARN: Current package...
EmberDataActiveModelParser 这是的中间件,可以使用 ember-data 的使用的 API 端点。 您可以在 ember-data 的阅读有关数据格式的... embed :ids , embed_in_root : true attributes :id , :name has_many :tasks e
另外,`children`属性可以让你访问文档的子节点,而`attributes`则返回所有属性的字典。 **性能和优化** Kanna的性能经过优化,能够快速解析大型XML和HTML文档。它使用内存流解析技术,避免一次性加载整个文档到...
有关默认值,请参阅attributes/default.rb 。 食谱 默认 启动“安装”配方。 用法 在要安装 opencv 的系统上包含recipe[opencv] 。 许可证和作者 作者:Guilhem Lettron 版权所有:: 2013, Youscribe, Inc 根据 ...
API.RSS 您可以找到最佳的RESTful RSS体验。 API.RSS可以将Feed数据转换为很棒的API。 该API简化了您处理RSS,... " title " : " \" Pandemic, Infodemic \" : 2 Cartoon Characters Battling Fake News In Assam " ,
Methods and attributes from this plugin are available in your `Dangerfile` under the `todoist` namespace. 通过警告确保修改后的代码中没有剩余的TODOS todoist.warn_for_todos 通过使构建失败,确保...
Ember 嵌套 Rails ##Configure Rails 生成 ember-esque json # config/initializers/active_model_... attributes :id , :title , :body has_many :comments end # app/serializers/comment_serializer.rb cl