Hello all. I have built a web application with ruby and am also making
sure that it can work under mod_ruby. Everything so far has been quite
simple, however I am noticing behavior that I do not understand when
attempting to extend an existing class. Here is a test case to
demonstrate the behavior:
···
#!/usr/local/ruby/bin/ruby
class String
def test_me?
self.length > 0
end
end
we are in mod_ruby if $? is false
puts “Content-type: text/plain\n\n” unless not $0
print "String responds to test_me?: "
puts ‘a’.respond_to?(:test_me?)
Under traditional cgi (or the command line), the following is the result:
String responds to test_me?: true
Under mod_ruby, I receieve:
String responds to test_me?: false
Does anyone have an explanation for this? Thanks in advance!!
–frank
Frank Fejes wrote:
#!/usr/local/ruby/bin/ruby
class String
def test_me?
self.length > 0
end
end
we are in mod_ruby if $? is false
puts “Content-type: text/plain\n\n” unless not $0
print "String responds to test_me?: "
puts ‘a’.respond_to?(:test_me?)
This is not the String class your are looking for. In mod_ruby, the
scripts are wrapped in an anonymous module, so you are effectively
creating a new class called String, but inside module.
Try
puts String.new.respond_to?(:test_me?)
in your script.
How to get the desired behaviour? Move it out of your code and into a
required file. Then realize that this will affect all running Apache
instances and all applications served by them. Consider it a bad thing
all together, or require the file in the httpd.conf itself, so that
other hapless coders understand why the default String class is acting
funky without searching across the complete harddisk. (Hapless coders
include you in the future 
···
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
That makes sense. This requires a different solution then. What is the
generally accepted “ruby way” of writing basic test functions? For
example, what prompted this whole question is that I need to validate
date input. I want dates to be entered in the format YYYY-MM-DD so I
wrote a method:
def is_valid_date?(str)
end
I used it in my program to test the value of entered_date:
if is_valid_date? entered_date
That works, but it does not possess a ruby feel. I’d like to ask the
entered_date object if it’s a valid date. So, I extended String:
class String
def is_valid_date?
end
end
Now I can call:
if entered_date.is_valid_date?
But that does not work with mod_ruby and I feel funny about extending
such a fundamental class like String. 
So, what’s the ruby way of handling this? Thanks!
–frank
···
On Thu, 20 Feb 2003 13:47:27 -0600, Kent Dahl wrote:
Frank Fejes wrote:
#!/usr/local/ruby/bin/ruby
class String
def test_me?
self.length > 0
end
end
we are in mod_ruby if $? is false
puts “Content-type: text/plain\n\n” unless not $0
print "String responds to test_me?: " puts ‘a’.respond_to?(:test_me?)
This is not the String class your are looking for. In mod_ruby, the
scripts are wrapped in an anonymous module, so you are effectively
creating a new class called String, but inside module.
Try
puts String.new.respond_to?(:test_me?)
in your script.
How to get the desired behaviour? Move it out of your code and into a
required file. Then realize that this will affect all running Apache
instances and all applications served by them. Consider it a bad thing
all together, or require the file in the httpd.conf itself, so that
other hapless coders understand why the default String class is acting
funky without searching across the complete harddisk. (Hapless coders
include you in the future 
Perhaps a class method. This particular operation is not tied to a
particular object, but then if you wanted to do other operations on dates
(e.g. calculate the number of days between two dates) you could then have
instances representing a particular date.
class MyDate
def MyDate.is_valid?(str) # test if a string is valid
...
end
def initialize(str) # create a 'MyDate' object from string
...
end
end
today = "2003-02-21"
puts "hello" if MyDate.is_valid?(today)
Incidentally, you might want to look at date.rb in the Ruby standard
library, and date2 on RAA (I haven't used either)
Regards,
Brian.
···
On Sat, Feb 22, 2003 at 03:23:01AM +0900, Frank Fejes wrote:
class String
def is_valid_date?
<my test>
end
end
Now I can call:
<do something> if entered_date.is_valid_date?
But that does not work with mod_ruby and I feel funny about extending
such a fundamental class like String. 
So, what's the ruby way of handling this? Thanks!