[Wtr-general] Method Names as variables?

Keith Lancaster klancaster1957 at gmail.com
Tue Apr 18 20:03:41 EDT 2006


It might be helpful if you described what it is you are trying to
accomplish. I have a feeling that there are much easier ways to get what you
are looking for than the approach you are taking.

Then method I mentioned previously will in fact work for your line. It would
look like this:

if (todosdocs[i.to_s].send(propertie) == value


That said, a couple of quick notes on the code example:

1. Other than the browser object, it does not appear from the code sample
that there is a need to make other variables global (starting with $).
2. You can create the array vector_fobjects simply by saying
       vector_fobjects = []
    You can certainly do Array.new instead, but there is no need to create
the object first and set it to nil. The same goes for the vector_search
variable. 
3. As someone else noted, you have not initialized todosdocs anywhere

Again without knowing exactly what you are trying for, you might try this:

# assume you have $browser some defined and initialized someplace
def ENRA_FindObjects(propertie, value)
    return_vector = []
    all_objects = $browser.document.body.all
    all_objects.each do |obj|
        return_vector << obj if obj.send(propertie) == value
   end
   return_vector
end

HTH,
Keith

(You will of course need to test this ­ I tried something similar in irb and
it looked like it would work)



On 4/18/06 3:27 PM, "Rodrigo Julian Martin" <rodrigo.martin at enratio.com>
wrote:

> Hello Keith! 
> Thanks for the quick answer, but I¹m afraid that this is not what I¹m looking
> forŠ
> I have written a function which search objects from a page, passing a
> propertie and a value as parametersŠ
>  
> This is the function:
>  
>  
> def ENRA_FindObjects(propertie, value)
>    $vector_fobjects=nil
>    $vector_fobjects=Array.new
>    j=0
>    alldocs=nil
>   alldocs=$browser.document.body.all
>   count=$browser.document.body.all.length
>    $vector_search=nil
>    $vector_search=Array.new
>    for i in 0..count-1 do
>       if (todosdocs[i.to_s].²#{propertie}=="#{value}")
>            $vector_search[j]=todosdocs[i.to_s]
>            j+=1
>        end
>    end
>    return($vector_search)
> end
>  
> So, I load a page with a browser, and execute this function with, let¹s say:
>  
> array_objects=ENRA_FindObjects(³className²,²button²)
>  
> I hope the question is more clear nowŠ
>  
> How can I write this properly? à todosdocs[i.to_s].²#{propertie}=="#{value}" ,
> In order that the propertie could be defined by a parameter?
>  
>  
> Thank you so much for your timeŠ
> Cheers
>  
> Rodrigo Julian Martin
> 
> 
> From: Keith Lancaster [mailto:klancaster1957 at gmail.com]
> Sent: Martes, 18 de Abril de 2006 05:17 p.m.
> To: wtr-general at rubyforge.org; Rodrigo Julian Martin
> Subject: Re: [Wtr-general] Method Names as variables?
>  
> I am assuming Item(80) in your example is an object.
> 
> In Ruby, you can use the ³send² method to call a function.
> 
> Example:
> 
> class MyClass
>     def my_function
>         puts ³in my function²
>     end
> end
> 
> c = MyClass.new
> function_name = ³my_function²
> c.send(function_name)
> 
> HTH,
> Keith
> 
> 
> 
> On 4/18/06 3:00 PM, "Rodrigo Julian Martin" <rodrigo.martin at enratio.com>
> wrote:
> Hello!
>  
> I have another code questionŠ
>  
> Can I call an object method by using a variable?
>  
> I¹ve tried something like this:
>  
>  
> propertie=²className²
> a = Item(80).¹#{propertie}¹
>  
> And obviously doesn¹t workŠ
>  
> I¹ve also tried: 
>  
> a = Item(80).{propertie}
>  
> but this is a call to a hashŠ
>  
>  
> Any help would be really appreciatedŠ
> Thanks in Advance!
>  
>  
> Rodrigo Julian Martin
> 
> 
> _______________________________________________
> Wtr-general mailing list
> Wtr-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/wtr-general
>  
> 
> 
> _______________________________________________
> Wtr-general mailing list
> Wtr-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/wtr-general


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20060418/930a67e4/attachment.html 


More information about the Wtr-general mailing list