# How do I setup x.to\_amethod where x is an integer?

**URL:** <https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497>\
**Category:** Uncategorized\
**Created:** [July 7, 2021, 5:36pm UTC](https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497 "2021-07-07T17:36:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thelmalu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@thelmalu](https://www.ruby-forum.com/u/thelmalu)\
**Post date:** [July 7, 2021, 5:36pm UTC](https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497/1 "2021-07-07T17:36:31Z")

</div>

I’m going through the Ruby Track on Exercism and the exercise for converting arabic to roman numerals makes assertions on x.to\_roman where x is an integer and to\_roman is the translation method. I can tackle the translation, but I have no idea of how to design the x.to\_method call: it doesn’t seem to involve a class or a module. The tests give to\_roman no parameter, so what do I setup so that it knows what integer to translate?

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [July 7, 2021, 10:03pm UTC](https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497/2 "2021-07-07T22:03:21Z")

</div>

Are you looking to add a method to a class? The following adds `to_roman` as a method on the class of Integers. `self` in the method is the integer.

```auto
2.7.2 :001 > class Integer
2.7.2 :002 > def to_roman()
2.7.2 :003 > puts "Current number #{self}"
2.7.2 :004 > end
2.7.2 :005 > end
 => :to_roman 
2.7.2 :006 > 3.to_roman()
Current number 3

```

---

<div class="post-metadata">

**Author:** ![thelmalu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@thelmalu](https://www.ruby-forum.com/u/thelmalu)\
**Post date:** [July 7, 2021, 10:43pm UTC](https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497/3 "2021-07-07T22:43:26Z")

</div>

Thank you. I didn’t know that you could add methods to classes not your own. How freely can you do this?

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [July 7, 2021, 11:49pm UTC](https://www.ruby-forum.com/t/how-do-i-setup-x-to-amethod-where-x-is-an-integer/261497/4 "2021-07-07T23:49:41Z")

</div>

> [@thelmalu](#):
>
> I didn’t know that you could add methods to classes not your own. How freely can you do this?

Yes, Ruby’s classes are known as ‘Open Classes’, and adding methods like this is ‘monkey patching’. It’s a standard meta-programming technique in Ruby.

You can do this anytime, to any class (at least, I don’t know of any way to stop it):

```auto
2.7.2 :001 > class A
2.7.2 :002 > def me()
2.7.2 :003 > puts "in A"
2.7.2 :004 > end
2.7.2 :005 > end
2.7.2 :006 > x = A.new
 => #<A:0x000055c3d6f25338> 
2.7.2 :007 > x.me()
in A
 => nil 
2.7.2 :008 > class A
2.7.2 :009 > def me_too()
2.7.2 :010 > puts "also in A"
2.7.2 :011 > end
2.7.2 :012 > end
 => :me_too 
2.7.2 :013 > x.me_too()
also in A
 => nil 

```

notice this works even though x was created before the method was defined.
