Help me please

hi, i’m the beginner in ruby. My task is change this version of code to
nonarguments method of Fixnum class using yield. That’ll be passed if i
could typing for example 12.divisions{|x| puts x} and it will be works.
I try it a lot but that’s my first time in ruby… and this is my
version for method with argue

def divisions(n)
i=1
while i <= n
if n%i>0
i += 1
else
yield n/i
i += 1
end
end
end

What i must change?

Hi Luc,

You need to add it to the Fixnum class, and use self to get at the
value, like so:

—===<<< SNIP >>>===—

class Fixnum
def divisions
n = self
i=1
while i <= n
if n%i>0
i += 1
else
yield n/i
i += 1
end
end
end
end

12.divisions {|x| puts x}

—===<<< SNIP >>>===—

I haven’t checked your algorithm, just made the changes needed to get it
to work the way you want.

HTH

Garth

PS. Don’t forget to indent. :wink:

Thank you Garthy it’s very helpfull for me. and i’ll remember about
indent:)

Hi Luk,

Happy to help. :slight_smile:

Garth

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