Don´t work my program :( , please help

67 views
Skip to first unread message

Richard Zanabria

unread,
Jul 11, 2015, 8:40:56 AM7/11/15
to rubyonra...@googlegroups.com
=begin
Pregunta 1
Desarrolle sus clases en Ruby que permita realizar el siguiente
programa:
auto = Auto.new(“Volvo”, 19,000)
neto = auto.calcular_neto(2)
puts “Neto: #{neto}” donde el método calcular_neto(2)
debe calcular el costo de dos autos Volvo con precio 19,000
y además le suma IGV que es una constante 0.18.
=end

class Auto

attr_accessor :auto01, :auto02

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

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

end

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

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jul 11, 2015, 8:59:53 AM7/11/15
to rubyonra...@googlegroups.com
On 11 July 2015 at 13:40, Richard Zanabria <li...@ruby-forum.com> 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

Richard Zanabria

unread,
Jul 11, 2015, 9:19:58 AM7/11/15
to rubyonra...@googlegroups.com
Thanks Colin, i foloow your advice.

One question:

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


Thank you very much for your help

Colin Law

unread,
Jul 11, 2015, 11:01:35 AM7/11/15
to rubyonra...@googlegroups.com
On 11 July 2015 at 14:19, Richard Zanabria <li...@ruby-forum.com> wrote:
> Thanks Colin, i foloow your advice.
>
> One question:
>
> 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

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

Taras Matsyk

unread,
Jul 12, 2015, 4:52:28 AM7/12/15
to rubyonra...@googlegroups.com
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 work in ruby and what attr_accessor key word 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

Colin Law

unread,
Jul 12, 2015, 8:54:08 AM7/12/15
to rubyonra...@googlegroups.com
On 12 July 2015 at 09:51, Taras Matsyk <li...@ruby-forum.com> 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

Richard Zanabria

unread,
Jul 12, 2015, 1:43:10 PM7/12/15
to rubyonra...@googlegroups.com
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}"

Colin Law

unread,
Jul 12, 2015, 2:37:39 PM7/12/15
to rubyonra...@googlegroups.com
On 12 July 2015 at 18:41, Richard Zanabria <li...@ruby-forum.com> 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

Elizabeth McGurty

unread,
Jul 12, 2015, 4:59:00 PM7/12/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Jul 12, 2015, 5:02:34 PM7/12/15
to rubyonra...@googlegroups.com
On 12 July 2015 at 21:58, Elizabeth McGurty <emcg...@gmail.com> 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

Richard Zanabria

unread,
Jul 12, 2015, 6:01:30 PM7/12/15
to rubyonra...@googlegroups.com
Thanks a todos por sus respuestas

Elizabeth McGurty

unread,
Jul 12, 2015, 6:33:14 PM7/12/15
to rubyonra...@googlegroups.com
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  

Richard Zanabria

unread,
Jul 13, 2015, 1:43:02 AM7/13/15
to rubyonra...@googlegroups.com
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}"

=====

Colin Law

unread,
Jul 13, 2015, 3:33:18 AM7/13/15
to rubyonra...@googlegroups.com
On 13 July 2015 at 06:42, Richard Zanabria <li...@ruby-forum.com> 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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/74f94cee3a088009009ff6357323e340%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages