Sunday, 26 February 2017

Ruby



1) rand(99) ->  9  produces random number

---------------------------------------------------------------------------
2)attr_accessor :var
attr_reader :var 
attr_writer :var

You may use the different accessors to communicate your intent to someone reading your code, and make it easier to write classes which will work correctly no matter how their public API is called.
class Person
  attr_accessor :age
  ...
end
Here, I can see that I may both read and write the age.
class Person
  attr_reader :age
  ...
end
Here, I can see that I may only read the age. Imagine that it is set by the constructor of this class and after that remains constant. If there were a mutator (writer) for age and the class were written assuming that age, once set, does not change, then a bug could result from code calling that mutator.
But what is happening behind the scenes?
If you write:
attr_writer :age
That gets translated into:
def age=(value)
  @age = value
end
If you write:
attr_reader :age
That gets translated into:
def age
  @age
end
If you write:
attr_accessor :age
That gets translated into:
def age=(value)
  @age = value
end

def age
  @age
end
---------------------------------------------------------------------

Hash

https://docs.ruby-lang.org/en/2.0.0/Hash.html

-------------------------------------------



No comments:

Post a Comment