Forum: Ruby Ruby multiplication

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca7f32f26eae605d4d16c46fc28ff750?d=identicon&s=25 Mi Gatt (migatt)
on 2017-03-13 10:44
Sorry if I'm in the incorrect forum, but I'm new here :)

I would need a function "multiplication" which returns the product of
her parametres "a" and "b".

Can anybody help me please?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-03-13 11:40
Mi Gatt wrote in post #1185675:
> Sorry if I'm in the incorrect forum, but I'm new here :)
>
> I would need a function "multiplication" which returns the product of
> her parametres "a" and "b".
>
> Can anybody help me please?

def mult(a,b)
 res=0
 while true
   return(res) if b<=0
   res=res+a
   b=b-1
 end
end
p mult 7,7
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-03-13 13:10
def add(a,b)
  sa='-'*a
  sb='-'*b
  return "#{sa}#{sb}".size
end
Ca7f32f26eae605d4d16c46fc28ff750?d=identicon&s=25 Mi Gatt (migatt)
on 2017-03-13 15:55
Thanks that helped me!!
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-03-14 07:00
def multiplication(a,b)
  a*b
end
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-03-14 07:01
Regis d'Aubarede wrote in post #1185677:
> def add(a,b)
>   sa='-'*a
>   sb='-'*b
>   return "#{sa}#{sb}".size
> end

Ah, I *love* this. This is by far the best answer to the question.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-03-17 12:33
Ronald Fischer wrote in post #1185684:
> Ah, I *love* this. This is by far the best answer to the question.

==
def n(a) '-'*a  end
def s(a) a.size end

def add(a,b)  return "#{a}#{b}" end
def moin(a,b)  return a[0..-s(add(b,'-'))] end

def mult(a,b)
 res=''
 loop {
   return(res)  if b==''
   res =add(a,res)
   b=moin(b,'-')
 }
end

p s( add('----','--')  )
p s( add  n(22), n(33) )
p s( moin n(22), n(1)  )
p s( moin n(22), n(23) )
p s( mult n(3) , n(5)  )
This topic is locked and can not be replied to.