Help with too many methods

I am writing a Ruby program that creates different URL queries. Right
now I have way too many methods to achieve this. Is there a way to do
this in Ruby to eliminate the need for so many methods?

Each of these method calls a create_url method that concatenates a
string representing the URL.

like this:

def getUse(username)
create_url(username)
end

def getPage(page)
create_url(page)
end

There are like 150 - 200 methods like this.

could you spare a paradigm?

Sam

On 15.08.2010 12:25, Samuel S. wrote:

I am writing a Ruby program that creates different URL queries. Right
now I have way too many methods to achieve this. Is there a way to do
this in Ruby to eliminate the need for so many methods?

Without further information about what all these methods do it’s
difficult to come up with good suggestions. For the case below see my
answer below.

create_url(page)

end

There are like 150 - 200 methods like this.

could you spare a paradigm?

There is no point in having methods like above: they don’t do anything
other than forwarding the parameter. Just get rid of them.

Btw, according to Ruby naming convention methods should be named
“get_use” and “get_page”.

Kind regards

robert

Thanks Robert.

I can see now I should just go back and hit the Ruby books.

Just found out about the Ruby pickaxe in another post. I am going to
give that a try.

Sam

Here’s one approach:

def get(name,value)
url_method_name = “create_#{name}”.to_sym
method = # look up create_xxx method, I can’t recall the syntax for
this
method.call(value)
end

Thank you very much. This will work excellently.

On 15.08.2010 14:28, Andrew W. wrote:

Here’s one approach:

def get(name,value)
url_method_name = “create_#{name}”.to_sym
method = # look up create_xxx method, I can’t recall the syntax for this
method.call(value)
end

I’d rather use a Hash and lambdas for that since it is more explicit and
more efficient:

Note, this is just unsafe dummy URL creation functionality

CREATORS = {
“foo” => lambda {|val| “http:/foo/#{val}”},
“bar” => lambda {|val| “http:/google.com/q=#{val}”},
}

def get(name, value)
CREATORS.fetch name do |key|
raise ArgumentError, “Cannot find %p” % key
end.call value
end

Kind regards

robert

On Sun, Aug 15, 2010 at 07:28, Andrew W. [email protected]
wrote:

Here’s one approach:

def get(name,value)
 url_method_name = “create_#{name}”.to_sym
 method = # look up create_xxx method, I can’t recall the syntax for this
 method.call(value)
end

That would be

def get(name,value)
url_method_name = “create_#{name}”.to_sym
 method = self.method(url_method_name)
 method.call(value)
end

If you are inside the class; if you are not, first get an object of
said class and replace ‘self’ for the variable in which you put it.

Thanks. I was on my way out the door when I wrote this and didn’t have
time to play with it.

On 8/15/10 12:48 PM, Samuel S. wrote:

Just found out about the Ruby pickaxe in another post. I am going to
give that a try.

Take a look to The Well-Grounded Rubyist (Manning, David Black), too.

The Well-Grounded Rubyist

I like the way it’s written.

P.S. first post here, hi all!

Ah, good call. That’s my bad, not Pablo’s.

Pablo Torres N. wrote:

That would be

def get(name,value)
url_method_name = “create_#{name}”.to_sym
 method = self.method(url_method_name)
 method.call(value)
end

There’s no need to create a Method object here, just use send:

def get(name, value)
send(“create_#{name}”, value)
end

(or “self.send” if you think that reads better)