How do I call a method with a period and pass the object before it?

like 1.to_s , except I want 8.bits_to_bytes

I know how to do it like bits_to_bytes(8) but I think that does not look
nearly as good. I know this is possible but I only pass the
bits_to_bytes(8) way so far.

i try 8.bits_to_bytes but it of course says that I am not providing a
parameter and one is required.

I have a lot of numbers and I need to work with both bits and byte
representations of them depending on the exact circumstances, and in the
process of switching from magic
numbers to ConstantNames I thought it looked ugly to have so many
constant names and then / 8 when it is done, I would rather have
(BitsInFile - BitsToSubstract).bits_to_bytes (for a random although not
very good example)
since that seems more consistent

Hi,

You have to define a “bits_to_bytes” method in the Integer class:

class Integer
def bits_to_bytes
# your code
end
end

puts 8.bits_to_bytes

However, it’s probably not a good idea to clutter the Integer class with
all kinds of special methods. Especially since “it looks better” isn’t
really an argument.

I’d rather put the “bits_to_bytes” into a module like “BitOperations” or
so. Then you can call it by “BitOperations.bits_to_bytes(8)”

Oh, what i mean by point is the dot or period.

1.9.2p290 :011 > class Fixnum
1.9.2p290 :012?> def add_argument(arg)
1.9.2p290 :013?> return self+arg
1.9.2p290 :014?> end
1.9.2p290 :015?> end
=> nil
1.9.2p290 :016 > 5.add_argument(10)
=> 15

What you do is add the method you want to Fixnum(which is the 1.class
class), ruby have open classes so you can add methods on the fly for it.

Now create the method, using the example for 5.add_argument(10), 5 is
self,
10 is arg. So to “get the number before the point” use self.

In order to chain methods(in case you want to do
5.add_argument(10).drink_coke.have_fun, return self.

Thiago M. wrote in post #1067793:

Oh, what i mean by point is the dot or period.

And that’s exactly what you’ve been shown.

If you want “obj.meth” to execute, then you need to define ‘meth’ within
the class of obj (or within the singleton class of obj).

8 is a Fixnum (which is an Integer which is a Numeric). Since you may
want this to work for Bignums too, then Integer is probably the right
place to define it, since Integer is the common superclass of both
Fixnum and Bignum.

class Integer
def bits_to_bytes
self/8
end
end

puts 16.bits_to_bytes # => 2

This is exactly what I was looking for. Thanks !
In other lingo 5 is the receiver, and add_argument(10) is the message
correct ?

Thiago M. wrote in post #1067792:

1.9.2p290 :011 > class Fixnum
1.9.2p290 :012?> def add_argument(arg)
1.9.2p290 :013?> return self+arg
1.9.2p290 :014?> end
1.9.2p290 :015?> end
=> nil
1.9.2p290 :016 > 5.add_argument(10)
=> 15

What you do is add the method you want to Fixnum(which is the 1.class
class), ruby have open classes so you can add methods on the fly for it.

Now create the method, using the example for 5.add_argument(10), 5 is
self,
10 is arg. So to “get the number before the point” use self.

In order to chain methods(in case you want to do
5.add_argument(10).drink_coke.have_fun, return self.