[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Modules: The Secret H method

Leslie Viljoen

2/19/2008 7:44:00 AM

Hello!

I am having a hard time using modules. It seems that to successfully
use one, you
need to know all sorts of obscure things about the environment that
the module expects.

For example, I want to use Webby::Helpers::UrlHelper. This module
refers to a @pages
variable that I have to provide in the class that "include"s the
module. That is not so
hard, but I can only find that requirement via experimentation. If I
provide the @pages
variable in my class, I then find that the module refers to a method
"h". I know rails
has a helper method "h", but cannot otherwise find one, despite all my
grep -ir "def h" on the entire standard library and all gems (well,
"rack" has such
a method too, but Webby never uses rack AFAIK).

So is this how people normally use modules, or am I doing something stupid?
How is anyone supposed to know the possibly highly complex
requirements of a module
- or does it fall to documentation again?

There's a similar problem with calling *any* methods that take
parameters in Ruby -
how can you get a list of all the methods that will be applied to the
object you passed
in? Either the documentation says "pass in a string" and then you are expected
to pass in something that responds to ALL of string's methods, or the
documentation
must list every method applied, along with its signature. All the
documentation I have
seen uses option A: it mentions the class to pass in. Which renders
duck-typing shot,
unless I descend from that class perhaps?

BTW: where can I find the secret "h" method?

Les

8 Answers

7stud --

2/19/2008 9:10:00 AM

0

Leslie Viljoen wrote:
> Hello!
>
> I am having a hard time using modules. It seems that to successfully
> use one, you
> need to know all sorts of obscure things about the environment that
> the module expects.
>
> For example, I want to use Webby::Helpers::UrlHelper. This module
> refers to a @pages
> variable that I have to provide in the class that "include"s the
> module. That is not so
> hard, but I can only find that requirement via experimentation. If I
> provide the @pages
> variable in my class, I then find that the module refers to a method
> "h". I know rails
> has a helper method "h", but cannot otherwise find one, despite all my
> grep -ir "def h" on the entire standard library and all gems (well,
> "rack" has such
> a method too, but Webby never uses rack AFAIK).
>
> So is this how people normally use modules, or am I doing something
> stupid?
> How is anyone supposed to know the possibly highly complex
> requirements of a module
> - or does it fall to documentation again?
>
> There's a similar problem with calling *any* methods that take
> parameters in Ruby -
> how can you get a list of all the methods that will be applied to the
> object you passed
> in? Either the documentation says "pass in a string" and then you are
> expected
> to pass in something that responds to ALL of string's methods, or the
> documentation
> must list every method applied, along with its signature. All the
> documentation I have
> seen uses option A: it mentions the class to pass in. Which renders
> duck-typing shot,
> unless I descend from that class perhaps?
>
> BTW: where can I find the secret "h" method?
>
> Les


This is ruby code:

puts 'hello'

--output:--
hello


This is also a ruby code:

#my_own_language.rb:
def puts(x)
print "goodbye\n"
end


#my_program.rb
require 'my_own_language.rb

puts 'hello'

--output:--
goodbye



How would you respond to someone who constantly asks why puts in Ruby
always outputs 'goodbye'?

--
Posted via http://www.ruby-....

Leslie Viljoen

2/19/2008 11:30:00 AM

0

> How would you respond to someone who constantly asks why puts in Ruby
> always outputs 'goodbye'?

I would say "because you redefined puts".

But this does not help me find the "h" method. It also does not help
me to discover the needs of a module or method.

Rimantas Liubertas

2/19/2008 11:58:00 AM

0

> I then find that the module refers to a method
> "h". I know rails
> has a helper method "h", but cannot otherwise find one, despite all my
> grep -ir "def h" on the entire standard library and all gems

That's because h is an alias for html_escape (defined in ERB::Util).
Do grep "alias h html_escape" on your standart library and you will
most likely find it.
Take a look at
actionpack/lib/action_view/template_handlers/erb.rb in rails and
/usr/lib/ruby/1.8/erb.rb in ruby standard lib (your paths may differ).

Regards,
Rimantas
--
http://rim...

Radoslaw Bulat

2/19/2008 12:38:00 PM

0

RnJvbSBteSBleHBlcmllbmNlIHdpdGggUnVieSBJIGNhbiBzYXkgdGhhdCBwZW9wbGUgb2Z0ZW4g
anVzdCBvdmVydXNlClJ1YnkgbW9kdWxlcyAoYW5kIGdlbmVyYWxseSBhbGwgUnVieSBtZXRhcHJv
Z3JhbW1pbmcgc3R1ZmYgYW5kIGR5bmFtaWMKY2FwYWJpbGl0aWVzKS4gSSB0aGluayB0aGF0IHVz
aW5nIG1vZHVsZXMgc2hvdWxkIGJlIHNpbWlsYXIgdG8KRW51bWVyYWJsZS4gSnVzdCBpbmNsdWRl
IGl0LCBkZWZpbmUgb25lICh0d28pIHNwZWNpYWwgbWV0aG9kcyAoZWFjaCwKPD0+KSBhbmQgeW91
IGhhdmUgc29tZSBmdW5jdGlvbmFsaXR5LiBCdXQgUnVieSBwcm9ncmFtbWVycyAoSSdtIFJ1YnkK
cHJvZ3JhbW1lciB0byA6KSkgdGVuZCB0byB1c2UgbW9kdWxlcyB0byBkaXN0cmlidXRlIGNsYXNz
CmltcGxlbWVudGF0aW9ucyB0byBkaWZmcmVudCBwYXJ0IChzbyB0aGV5IHVzZSBtYW55IGluc3Rh
bmNlIHZhcmlhYmxlcwphbmQgaW50ZXJuYWwgcGFydCBvZiBkZXNpcmVkIGNsYXNzKS4KCkZvciBl
eGFtcGxlLCB3aGVuIEkgd2FudCB0byBmaW5kIHdoYXQgbWV0aG9kcyBhcmUgaW4gY29udHJvbGxl
cgppbnN0YW5jZSAocmFpbHMpIEkgY2FuJ3QganVzdCBnbyB0byBBY3Rpb25Db250cm9sbGVyOjpC
YXNlIGFuZCBsaXN0CmFsbCBtZXRob2RzIGJlY2F1c2UgdGhhdCBjbGFzcyBoYXMgbWFueSBpbmNs
dWRlZCBtb2R1bGVzIChhbmQgbWFueSBvZgp0aGVtIGFyZSBpbmNsdWRlZCBvdXRzaWRlIGNsYXNz
KS4gSSdtIG5vdCBjb21wbGFpbmluZyBhYm91dCBSdWJ5Cml0c2VsZiwgcmF0aGVyIGFib3V0IHBy
b2dyYW1tZXJzIDopLgoKLS0gClJhZG9zs2F3IEJ1s2F0CgpodHRwOi8vcmFkYXJlay5qb2dnZXIu
cGwgLSBt82ogYmxvZwo=

Leslie Viljoen

2/19/2008 12:40:00 PM

0

On Feb 19, 2008 1:58 PM, Rimantas Liubertas <rimantas@gmail.com> wrote:
> > I then find that the module refers to a method
> > "h". I know rails
> > has a helper method "h", but cannot otherwise find one, despite all my
> > grep -ir "def h" on the entire standard library and all gems
>
> That's because h is an alias for html_escape (defined in ERB::Util).
> Do grep "alias h html_escape" on your standart library and you will
> most likely find it.
> Take a look at
> actionpack/lib/action_view/template_handlers/erb.rb in rails and
> /usr/lib/ruby/1.8/erb.rb in ruby standard lib (your paths may differ).

Ah, thank-you very much, that helps a lot.

7stud --

2/19/2008 12:48:00 PM

0

Leslie Viljoen wrote:
>> How would you respond to someone who constantly asks why puts in Ruby
>> always outputs 'goodbye'?
>
> I would say "because you redefined puts".
>
> But this does not help me find the "h" method.

Then consider directing your questions to whoever wrote
my_own_language.rb or the people who use it frequently.

--
Posted via http://www.ruby-....

Leslie Viljoen

2/19/2008 1:04:00 PM

0

On Feb 19, 2008 2:48 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Leslie Viljoen wrote:
> >> How would you respond to someone who constantly asks why puts in Ruby
> >> always outputs 'goodbye'?
> >
> > I would say "because you redefined puts".
> >
> > But this does not help me find the "h" method.
>
> Then consider directing your questions to whoever wrote
> my_own_language.rb or the people who use it frequently.

Yes, the 'h' method is in ERB. ERB is part of standard Ruby.
This is the Ruby mailing list.

John Wilger

2/19/2008 1:39:00 PM

0

On Feb 18, 11:43 pm, Leslie Viljoen <leslievilj...@gmail.com> wrote:
> Hello!
>
> I am having a hard time using modules. It seems that to successfully
> use one, you
> need to know all sorts of obscure things about the environment that
> the module expects.
>
> For example, I want to use Webby::Helpers::UrlHelper. This module
> refers to a @pages
> variable that I have to provide in the class that "include"s the
> module. That is not so
> hard, but I can only find that requirement via experimentation. If I
> provide the @pages
> variable in my class

This, honestly is a sign of bad design. Modules should be designed so
that the including class only needs to define a few--if any--methods to
be used by that module, and they should be clearly documented in the
module. Accessing instance variables that are not "owned" by the
module is just asking for trouble. No, I don't think /you're/ doing
something wrong--your just running into a poorly designed module.

> There's a similar problem with calling *any* methods that take
> parameters in Ruby -
> how can you get a list of all the methods that will be applied to the
> object you passed
> in?

Other than by looking at the code, you can't, really. I suppose you
could always pass in some sort of "recorder" object to list out the
method calls, but I'm not sure the returns would be worth it in most
cases. Honestly, this is where a good automated test suite will help
you. Just verify in your tests that whatever kind of object you're
actually going to pass in doesn't cause any errors.

--
Regards,

John Wilger