[Wtr-general] Method Names as variables?
Keith Lancaster
klancaster1957 at gmail.com
Wed Apr 19 11:13:28 EDT 2006
Glad to help. Have fun with Ruby - its a great language and a good one for
learning object-oriented programming.
Keith
On 4/19/06, Rodrigo Julian Martin <rodrigo.martin at enratio.com> wrote:
>
> Hello Keith!
>
> The sample code you wrote works just fine!
>
>
>
> I hadn't understand the .send until I saw it on your example..
>
> And the tips for the Array creation and the iteration of all objects with
> .each |do| really like me..
>
> As you can see from my code I'm more of structured programming, but I hope
> to develop a more Ruby style in my code… and definitively you and many more
> people in this list are helping!
>
>
>
> Sorry for my bad english, and thanks again!
>
>
> ------------------------------
>
> *From:* wtr-general-bounces at rubyforge.org [mailto:
> wtr-general-bounces at rubyforge.org] *On Behalf Of *Keith Lancaster
> *Sent:* Martes, 18 de Abril de 2006 09:04 p.m.
>
> *To:* wtr-general at rubyforge.org
> *Subject:* Re: [Wtr-general] Method Names as variables?
>
>
>
> 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]<klancaster1957 at gmail.com%5d>
> *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
>
>
>
> _______________________________________________
> 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/20060419/a0ffe562/attachment.html
More information about the Wtr-general
mailing list