How do I call a method with a periodt 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.

···

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

You would have to either:
1. define an instance method on the class of whatever goes before the
period, e.g.:

class Fixnum
  def bits_to_bytes
    # method content here; `self` holds the object under consideration
  end
end

OR
2. define a singleton method on the actual object that goes before the
period, e.g.:

def 8.bits_to_bytes
  # method content here; `self` also holds the object under consideration
end

You probably want option #1 since it will work with all Fixnums.

···

On Sat, Jul 7, 2012 at 6:05 AM, roob noob <lists@ruby-forum.com> wrote:

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.