How Can I Do This?

I have two methods that do the same thing in different ways.
Let’s call them method ‘a’ and method ‘b’.

I currently have two other methods ‘ma’ and ‘mb’ which are identical
except where one uses ‘a’ and the other uses ‘b’.

So instead of creating the two methods ‘ma’ and ‘mb’ which are identical
in every way except for one place in the code which does in ‘ma’ -> x.a
and in ‘mb’ -> x.b can I pass in the methods ‘a’ and ‘b’ into the code
so I don’t have to create two different versions?

In other words, is it possible to do something like this:

def m

x.method
end

ma = m.method a
mb = m.method b

and them use ‘ma’ and ‘mb’ as before.

class A

def a(x) ‘a’+x end
def b(x) ‘b’+x end
def m(name,x)

send(name,x)

end
def ma(x) m(‘a’,x) end
def mb(x) m(‘b’,x) end
end

Regis d’Aubarede wrote in post #1172202:

class A

def a(x) ‘a’+x end
def b(x) ‘b’+x end
def m(name,x)

send(name,x)

end
def ma(x) m(‘a’,x) end
def mb(x) m(‘b’,x) end
end

This isn’t exactly what I need.

I need a constructor process that will take just a method name (with no
parameters for it) and apply it to one value within the constructor.

def m( …)

x.apply_method
end

So method ‘m’ takes an existing method, which operates on ‘x’ in each
case.

The constructed methods actually takes integers values like this:

y1.ma y2

where ma uses method ‘a’, etc, to operate on ‘x’ within it.

So at the point in ‘m’ where it does - x.apply_method - I want it to
have
the same affect as if I wrote it explicitly as x.a or x.b.

And methods ‘a’ and ‘b’ return just true or false depending on value
‘x’.

So conceptually I want to define named methods using a constructor
method which takes a method name, and applies it internally in the
manner shown

I hope that makes it clearer on what I’m trying to do.

Jabari Z. wrote in post #1172221:

I need a constructor process that will take just a method name (with no
parameters for it) and apply it to one value within the constructor.

def m( …)

x.apply_method
end

def m(mname,…)

x.send(mname)
end
m(‘a’,…)
m(‘b’,…)

So conceptually I want to define named methods using a constructor

you don’t need a ‘constructor’: name of method is enough.
‘send’ is like ‘apply’ on other language.

I hope that makes it clearer on what I’m trying to do.
me too :slight_smile: