Method names and alias

D

darren kirby

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)
<snip 15 lines>
@registers.gen[destination] = bitstring_to_int(byte)
true
end

def load_byte_unsigned(destination, base_address, offset)
<snip 15 lines>
@registers.gen[destination] = bitstring_to_int(byte, signed=false)
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
 
H

hemant

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)
<snip 15 lines>
@registers.gen[destination] = bitstring_to_int(byte)
true
end

def load_byte_unsigned(destination, base_address, offset)
<snip 15 lines>
@registers.gen[destination] = bitstring_to_int(byte, signed=false)
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
z> 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...
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


--
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
 
P

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:

def load_byte( byte, unsigned=false )
#...your code here...
end

def load_byte_unsigned( byte )
load_byte( byte, true )
end

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

Peña, Botp

From: darren kirby [mailto:[email protected]]=20
# def load_byte(destination, base_address, offset)
# <snip 15 lines>
# @registers.gen[destination] =3D bitstring_to_int(byte)
# true
# end
#=20
# def load_byte_unsigned(destination, base_address, offset)
# <snip 15 lines>
# @registers.gen[destination] =3D bitstring_to_int(byte, =
signed=3Dfalse)
# true
# end

def load_byte(destination, base_address, offset, signed=3Dtrue)
<snip 15 lines>
@registers.gen[destination] =3D bitstring_to_int(byte,signed)
true
end

load_byte(destination, base_address, offset) #signed
load_byte(destination, base_address, offset, signed=3Dfalse) #unsigned
 
D

darren kirby

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

I'd personally do something like:

def load_byte( byte, unsigned=false )
#...your code here...
end

def load_byte_unsigned( byte )
load_byte( byte, true )
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top