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
···
--
Posted via http://www.ruby-forum.com/.
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
···
On 15.08.2010 12:25, Samuel Sternhagen wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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
···
On Sun, Aug 15, 2010 at 6:25 AM, Samuel Sternhagen <samatoms@gmail.com>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?
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
--
Posted via http://www.ruby-forum.com/.
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
···
--
Posted via http://www.ruby-forum.com/.
Thank you very much. This will work excellently.
···
--
Posted via http://www.ruby-forum.com/.
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 15.08.2010 14:28, Andrew Wagner 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
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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.
···
On Sun, Aug 15, 2010 at 07:28, Andrew Wagner <wagner.andrew@gmail.com> 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
Thanks. I was on my way out the door when I wrote this and didn't have time to play with it.
···
On Aug 15, 2010, at 12:45 PM, "Pablo Torres N." <tn.pablo@gmail.com> wrote:
On Sun, Aug 15, 2010 at 07:28, Andrew Wagner <wagner.andrew@gmail.com> 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.
Take a look to The Well-Grounded Rubyist (Manning, David Black), too.
http://www.manning.com/black2/
I like the way it's written.
P.S. first post here, hi all!
···
On 8/15/10 12:48 PM, Samuel Sternhagen wrote:
Just found out about the Ruby pickaxe in another post. I am going to
give that a try.
--
Stefano
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)
···
--
Posted via http://www.ruby-forum.com/.
Ah, good call. That's my bad, not Pablo's.
···
On Mon, Aug 16, 2010 at 10:19 AM, Brian Candler <b.candler@pobox.com> wrote:
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)
--
Posted via http://www.ruby-forum.com/.