Help with too many methods

S

Samuel Sternhagen

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
 
R

Robert Klemme

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.
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?

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
 
S

Samuel Sternhagen

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
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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
 
R

Robert Klemme

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
 
P

Pablo Torres N.

Here's one approach:

def get(name,value)
=C2=A0url_method_name =3D "create_#{name}".to_sym
=C2=A0method =3D # look up create_xxx method, I can't recall the syntax f= or this
=C2=A0method.call(value)
end

That would be

def get(name,value)
url_method_name =3D "create_#{name}".to_sym
=C2=A0method =3D self.method(url_method_name)
=C2=A0method.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.
 
A

Andrew Wagner

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

 
B

Brian Candler

Pablo said:
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)
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top