Explaining this simple method code

Hello,

I wanted to make a small command line script in Ruby. User inputs name
of Restaurant and type and average price. gets results for these add
info

a guy helped me to write simple class. I have tow questions from the
code below:

class Restaurant
attr_accessor :name :type :avg_price
def initialize(name, type, avg_price)
@name = name
@type = type
@avg_price = price
end
end

If we created the attr_accessors for the type, and price, and name
Why we need to use the Initialize method? because we need to set the
inputed values to it?

second question please:

in the code also i have a sub class called RestaurantList followed by <
Array
It seems to me it is a subclass. but the Array class doesn’t exist in
the code? is it a built in class in ruby called Array? what it does
exactly?

Please make your answers as simple as you can make it so i can
understand it easily.

Thanks.

On Fri, Jul 27, 2012 at 10:39 PM, Ruby S. [email protected] wrote:

attr_accessor :name :type :avg_price
inputed values to it?
You need #initialize only if you want to be able to set those values
during initialization. Otherwise you can as well create the instance
and set values then:

r = Restaurant.new
r.name = “foo”
r.type = “Italian”
r.avg_price = 14

Btw, you can also use Struct here

Restaurant = Struct.new :name, :type, :avg_price

Now you have a class Restaurant with the same accessor methods (and a
bit more, please see documentation).

second question please:

in the code also i have a sub class called RestaurantList followed by <
Array
It seems to me it is a subclass. but the Array class doesn’t exist in
the code? is it a built in class in ruby called Array? what it does
exactly?

http://rubydoc.info/stdlib/core/Array

Please make your answers as simple as you can make it so i can
understand it easily.

Well, you will have to invest some amount of effort if you want to
learn the language or to program at all. You can find more
documentation and also introductory material here:
http://www.ruby-lang.org/en/documentation/

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs