Don´t work my program :( , please help

class Auto

attr_accessor :auto

def initialize(auto)
@modelo = modelo
@precio = precio

end

def calcular_neto(i)
for i in 0 …i
(auto)*0.18
end
return auto

end

auto = Auto.new(“Volvo”, 19000)
neto = auto.calcular_neto(2)
puts " Neto: #{neto}"

On 11 July 2015 at 13:40, Richard Z. [email protected] wrote:

class Auto

attr_accessor :auto01, :auto02

def initialize(auto01,auto02)
auto01 = auto01
@auto02 = auto02

Why does the last line start @ and not the one before?

end

def calcular_neto
(auto01 + auto02)*0.18 + auto01 + auto02

Should there any @s in the above?

In future if there is an error please post the error message. Also it
was not necessary to post a multi line comment at the start that was
not of any significance to the question.

Colin

Thanks Colin, i foloow your advice.

One question:

class Auto

attr_accessor :auto01, :auto02

def initialize(auto01,auto02)
@auto01 = auto01
@auto02 = auto02
end

def calcular_neto(2)
(auto01 + auto02)*0.18 + auto01 + auto02
end

end

auto = Auto.new(19000, 19000)
neto = auto.calcular_neto(2)
puts " Neto: #{neto}"

========================================================
in:
def calcular_neto(2) # it´s necesary (2) , I dont know how its work
(2).

It is not enough that this ?

attr_accessor :auto01, :auto02

========================================================
Would have to be used “for” ?

Thank you very much for your help

Hi Richard,

You definitely missed a couple of important topics about OOP and
encapsulation in particular.

Take a look on attribute accessors in Java (as example), so you could
understand how it works in ruby and what attr_accessor keyword does.
This guy has a nice explanation (cannot say it about his english, so you
can look for similar guide in Spanish)
https://www.youtube.com/watch?v=VOR-uQCCbcw

You need more practice and at least one good book about the way ruby
works, because without any background you will be thinking that the
whole mess you can find in Ruby is the right way to do things.

Think about an example below:
class Auto

attr_accessor :auto01, :auto02

def initialize(auto01,auto02)
@auto01 = auto01
@auto02 = auto02
end

def calcular_neto
auto01 = 1
# what will happend here? what are local variables?
puts auto01
# why have I used “@” symbol. Why is it needed
puts @auto01
puts auto02
# Would be result the same if I changed auto01 to
@auto01
(auto01 + auto02)*0.18 + auto01 + auto02
end

    # think about this one. Why it has get prefix?
    # why do we need return? What does ruby return and when?
    # Is there a difference between "@" and "self"?
    # what will be the result?

def get_neto
return (@auto01 + @auto02)*0.18 + self.auto01 + self.auto02
end
end

Check it out please, and post here further questions

On 11 July 2015 at 14:19, Richard Z. [email protected] wrote:

attr_accessor :auto01, :auto02

In the initialiser you can either use @auto… or self.auto… The
latter will use the accessor. This is necessary even if you change
the parameter name in the initialiser so that it is not the same as
the accessor.
It seems that in fact you do not need either in calculator_neto but I
am not sure why it is necessary in the initialiser but is not
necessary there. Probably someone more knowledgeable will explain.

Colin

On 12 July 2015 at 09:51, Taras M. [email protected] wrote:


attr_accessor :auto01, :auto02

def calcular_neto
auto01 = 1

Taras, why does the above create a local variable rather than using
the accessor= method, whereas
x = auto02
picks up @auto02 using the accessor.

Colin

I would like the program receives as parameters the catidad volvo car
and demarcates the program calculate the cost plus impuesos (IGV) of the
desired amount of cars.

I tried to modify the program to try to introduce a “for” to iterate the
program depending on how many cars you wish to calculate

class Auto

attr_accessor :auto

def initialize(auto)
@modelo = modelo
@precio = precio

end

def calcular_neto(i)
for i in 0 …i
(auto)*0.18
end
return auto

end

auto = Auto.new(“Volvo”, 19000)
neto = auto.calcular_neto(2)
puts " Neto: #{neto}"

On 12 July 2015 at 18:41, Richard Z. [email protected] wrote:

def calcular_neto(i)
for i in 0 …i
(auto)*0.18

First I suggest that you could use puts within your code to try and
work out exactly which bit of code is not working. Usually you will
then see your mistake, otherwise you can come back and explain exactly
the problem. In this case you did not even tell us what was
happening. In this case you could output the value of i and check
that the result of the calculation is what you expect.

Secondly, though we have determined that it is ok to use accessor
methods when reading a variable within a member method I suggest that
you would be much better to use @auto here, what is the point in
wasting processor time calling the accessor method? Also it is clearer
for the reader to see exactly what you are doing.

Thirdly what is for i in 0…i supposed to do? Iterate i through the
values 0 to i?

Fourthly it is normal to do something with the result of a calculation
rather than just calculating it and ignoring the answer.

Fifthly I suggest you work right through a ruby tutorial. I am sure
there are many on the web.

Sixthly this is a Ruby on Rails list, questions are generally supposed
to have some relevance to Rails applications. I think there are
specific Ruby lists/forums if you are not using Rails.

Seventhly…THERE IS NO SEVENTHLY.

Colin

Okay, I have been following this for a while. I am writing here
respectfully of other contributors. It is just that I see the matter
differently, and I am wondering why none of you have suggested
inheritance.

You have a car. And that car has a model and has a price. Anyway in
your
application, you will store, one by one, a car model and its price.

Your class should look like this:

class Auto

attr_accessor :model, :price

def initialize(model_value, price_value)

@model = model_value
@price = price_value

end

end

That’s it for class Auto. You are basically using class Auto for
storage

Introduce a new class, using inheritance

class Auto_Calculations < Auto

def calculate_percent(...)
end

def calculate_percent_plus_itself(...)
end

end

Just seems to me that the failure in this fellow’s code is that he is
combining two data instances in one class.

I might me totally wrong… Just offering
Liz

On 12 July 2015 at 21:58, Elizabeth McGurty [email protected] wrote:

Okay, I have been following this for a while. I am writing here
respectfully of other contributors. It is just that I see the matter
differently, and I am wondering why none of you have suggested inheritance.

I do not think we have been discussing the best way to solve a
specific problem, but rather we have been discussing subtleties of the
Ruby language.

Colin

thanks everybody for yours answers

Point taken, however I beg to differ… However novice in ROR folks
maybe,
I think that they bring significance in background. As an educator I
have
found that appealing to more advanced notions, may be effective. We
just
have different styles.

From the get go all you needed to suggest was:

class Auto

attr_accessor :auto01, :auto02

def initialize(auto01_value,auto02_value)
@auto01 = auto01_value
@auto02 = auto02_value
end

def calcular_neto
(self.auto01 + self.auto02)*0.18 + self.auto01 + self.auto02
end

end

auto = Auto.new(19000, 19000)
neto = auto.calcular_neto
puts " Neto: #{neto}"

Then you could point out that car Model was not considered

Liz

On 13 July 2015 at 06:42, Richard Z. [email protected] wrote:

I try with this:

You do not seem to have answered any of the six points I made.
Particularly the third and fourth.

Colin

I try with this:

class Auto

attr_accessor :auto

def initialize(auto)
@modelo = modelo
@precio = precio

end

def calcular_neto(i)
for i in 0 …i
(auto)*0.18
end
return auto

end

auto = Auto.new(“Volvo”, 19000)
neto = auto.calcular_neto(2)
puts " Neto: #{neto}"

=====

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