# Method names and alias

**URL:** <https://rubytalk.org/t/method-names-and-alias/40298>\
**Category:** ruby-talk\
**Created:** [31 August 2007 19:47 UTC](https://rubytalk.org/t/method-names-and-alias/40298 "2007-08-31T19:47:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://rubytalk.org/u/darren_kirby)\
**Post date:** [31 August 2007 19:47 UTC](https://rubytalk.org/t/method-names-and-alias/40298/1 "2007-08-31T19:47:40Z")

</div>

Hello all,

In Unix, you can create a symlink to an executable, and have the program  
behave differently depending on how it was called by testing argv[0]. I would  
like to do something similar with Ruby methods using alias.

I have many methods which come in pairs, a version each for signed and  
unsigned numbers. These methods differ by only one line, eg:

def load\_byte(destination, base\_address, offset)  
&nbsp;&nbsp;\<snip 15 lines\>  
&nbsp;&nbsp;@registers.gen[destination] = bitstring\_to\_int(byte)  
&nbsp;&nbsp;&nbsp;true  
end

def load\_byte\_unsigned(destination, base\_address, offset)  
&nbsp;&nbsp;\<snip 15 lines\>  
&nbsp;&nbsp;@registers.gen[destination] = bitstring\_to\_int(byte, signed=false)  
&nbsp;&nbsp;&nbsp;true  
end

My code would be considerably DRYer if I could make 'load\_byte\_unsigned' an  
alias to 'load\_byte', and have the method inspect the name it was called as  
to decide if it should run 'bitstring\_to\_int(byte, signed=false)' or not.

Is there a way to do this?

If not, I suppose my best bet is to move the snipped 15 lines to  
a 'load\_byte\_common' method and make 'load\_byte' and 'load\_byte\_unsigned'  
thin wrappers around it?

Thanks for consideration,  
-d

> **···**
>
> --  
> darren kirby :: Part of the problem since 1976 :: [http://badcomputer.org](http://badcomputer.org)  
> "...the number of UNIX installations has grown to 10, with more expected..."  
> - Dennis Ritchie and Ken Thompson, June 1972

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [1 September 2007 04:10 UTC](https://rubytalk.org/t/method-names-and-alias/40298/2 "2007-09-01T04:10:10Z")

</div>

> Hello all,
> 
> In Unix, you can create a symlink to an executable, and have the program  
> behave differently depending on how it was called by testing argv[0]. I would  
> like to do something similar with Ruby methods using alias.
> 
> I have many methods which come in pairs, a version each for signed and  
> unsigned numbers. These methods differ by only one line, eg:
> 
> def load\_byte(destination, base\_address, offset)  
> &nbsp;&nbsp;\<snip 15 lines\>  
> &nbsp;&nbsp;@registers.gen[destination] = bitstring\_to\_int(byte)  
> &nbsp;&nbsp;&nbsp;true  
> end
> 
> def load\_byte\_unsigned(destination, base\_address, offset)  
> &nbsp;&nbsp;\<snip 15 lines\>  
> &nbsp;&nbsp;@registers.gen[destination] = bitstring\_to\_int(byte, signed=false)  
> &nbsp;&nbsp;&nbsp;true  
> end
> 
> My code would be considerably DRYer if I could make 'load\_byte\_unsigned' an  
> alias to 'load\_byte', and have the method inspect the name it was called as  
> to decide if it should run 'bitstring\_to\_int(byte, signed=false)' or not.

Well this is your problem, "have the method inspect the name it was  
called as". I guess if you really want to DRY them up ( although the  
other approach you suggested sounds good enough), you can use  
method\_missing and capture method names and do stuff based on that.

Also, if there is a way to determine method that you intend to call  
based on argument data type, then you can inspect your arguments and  
do DRY stuff. But i guess you would have already thought out that  
possibility. Anyways...

> **···**
>
> On 9/1/07, darren kirby \<bulliver@badcomputer.org\> wrote:
> 
> > Is there a way to do this?
> > 
> > If not, I suppose my best bet is to move the snipped 15 lines to  
> > a 'load\_byte\_common' method and make 'load\_byte' and 'load\_byte\_unsigned'  
> > thin wrappers around it?
> > 
> > Thanks for consideration,  
> > -d  
> > --  
> > darren kirby :: Part of the problem since 1976 :: [http://badcomputer.org](http://badcomputer.org)  
> > "...the number of UNIX installations has grown to 10, with more expected..."  
> > - Dennis Ritchie and Ken Thompson, June 1972
> 
> --  
> Let them talk of their oriental summer climes of everlasting  
> conservatories; give me the privilege of making my own summer with my  
> own coals.
> 
> [http://blog.gnufied.org](http://blog.gnufied.org)

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [1 September 2007 05:15 UTC](https://rubytalk.org/t/method-names-and-alias/40298/3 "2007-09-01T05:15:09Z")

</div>

> My code would be considerably DRYer if I could make 'load\_byte\_unsigned' an  
> alias to 'load\_byte', and have the method inspect the name it was called as  
> to decide if it should run 'bitstring\_to\_int(byte, signed=false)' or not.
> 
> Is there a way to do this?

I can't offer you a way to do this, but:

> If not, I suppose my best bet is to move the snipped 15 lines to  
> a 'load\_byte\_common' method and make 'load\_byte' and 'load\_byte\_unsigned'  
> thin wrappers around it?

I'd personally do something like:

&nbsp;&nbsp;def load\_byte( byte, unsigned=false )  
&nbsp;&nbsp;&nbsp;&nbsp;#...your code here...  
&nbsp;&nbsp;end

&nbsp;&nbsp;def load\_byte\_unsigned( byte )  
&nbsp;&nbsp;&nbsp;&nbsp;load\_byte( byte, true )  
&nbsp;&nbsp;end

A (private?) utility function is certainly possible, but not the way  
I'd go, or needed.

> **···**
>
> On Aug 31, 1:47 pm, darren kirby \<bulli...@badcomputer.org\> wrote:

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [1 September 2007 07:11 UTC](https://rubytalk.org/t/method-names-and-alias/40298/4 "2007-09-01T07:11:50Z")

</div>

# def load\_byte(destination, base\_address, offset)  
# \<snip 15 lines\>  
# @registers.gen[destination] = bitstring\_to\_int(byte)  
# true  
# end

> **···**
>
> From: darren kirby [[mailto:bulliver@badcomputer.org](mailto:bulliver@badcomputer.org)]  
> #  
> # def load\_byte\_unsigned(destination, base\_address, offset)  
> # \<snip 15 lines\>  
> # @registers.gen[destination] = bitstring\_to\_int(byte, signed=false)  
> # true  
> # end
> 
> def load\_byte(destination, base\_address, offset, signed=true)  
> &nbsp;&nbsp;&nbsp;\<snip 15 lines\>  
> &nbsp;&nbsp;&nbsp;@registers.gen[destination] = bitstring\_to\_int(byte,signed)  
> &nbsp;&nbsp;&nbsp;true  
> end
> 
> load\_byte(destination, base\_address, offset) #signed  
> load\_byte(destination, base\_address, offset, signed=false) #unsigned

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://rubytalk.org/u/darren_kirby)\
**Post date:** [1 September 2007 17:59 UTC](https://rubytalk.org/t/method-names-and-alias/40298/5 "2007-09-01T17:59:22Z")

</div>

quoth the Phrogz:

> \> My code would be considerably DRYer if I could make 'load\_byte\_unsigned'  
> \> an alias to 'load\_byte', and have the method inspect the name it was  
> \> called as to decide if it should run 'bitstring\_to\_int(byte,  
> \> signed=false)' or not.  
> \>  
> \> Is there a way to do this?
> 
> I can't offer you a way to do this, but:  
> \> If not, I suppose my best bet is to move the snipped 15 lines to  
> \> a 'load\_byte\_common' method and make 'load\_byte' and 'load\_byte\_unsigned'  
> \> thin wrappers around it?
> 
> I'd personally do something like:
> 
> &nbsp;&nbsp;def load\_byte( byte, unsigned=false )  
> &nbsp;&nbsp;&nbsp;&nbsp;#...your code here...  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def load\_byte\_unsigned( byte )  
> &nbsp;&nbsp;&nbsp;&nbsp;load\_byte( byte, true )  
> &nbsp;&nbsp;end

Thanks Phrogz,

This gives me some ideas on how to clean up the code a bit.

@Hemant:

I guess I was hoping there may have been some "if self.name == "load\_byte"  
magic that I could have used.

@Pena:

Thanks, but I cannot change the name of the method, that is, there must be an  
unsigned counterpart, as I am writing methods which emulate Mips32 ISA and I  
want my methods to match the ops...the real methos names are 'lb, and 'lbu'  
so...

-d

> **···**
>
> > On Aug 31, 1:47 pm, darren kirby \<bulli...@badcomputer.org\> wrote:
> 
> --  
> darren kirby :: Part of the problem since 1976 :: [http://badcomputer.org](http://badcomputer.org)  
> "...the number of UNIX installations has grown to 10, with more expected..."  
> - Dennis Ritchie and Ken Thompson, June 1972
