Modules: The Secret H method

L

Leslie Viljoen

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
 
7

7stud --

Leslie said:
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'?
 
L

Leslie Viljoen

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

Rimantas Liubertas

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
 
R

Rados³aw Bu³at

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

Leslie Viljoen

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

7stud --

Leslie said:
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.
 
L

Leslie Viljoen

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

John Wilger

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.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top