Attributes in Ruby

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 代码
 
  1. class Song  
  2. def duration_in_minutes  
  3. @duration/60.0 # force floating point  
  4. end  
  5. def duration_in_minutes=(new_duration)  
  6. @duration = (new_duration*60).to_i  
  7. end  
  8. end  
  9. song = Song.new("Bicylops""Fleck", 260)  
  10. song.duration_in_minutes ! 4.33333333333333  
  11. song.duration_in_minutes = 4.2  
  12. 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 代码
 
  1. def duration_in_minutes=(new_duration)    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值