[Note: parts of this message were removed to make it a legal post.]
On Sun, Jul 11, 2010 at 5:19 PM, Abder-rahman Ali <
I'm just new to ruby, and want to ask about what this means (I got it
from Why's poignant guide to Ruby):
http://pastie.org/private/wndhyi2zshltqzwippqb3q
I think it would be OK to put that code in your email: it's not very long.
z = :include?
z = "a string".respond_to? z
Interactive Ruby can be very useful in finding out what's going on.
irb
z = :include?
#=> :include?
z.class
#=> Symbol
So z is now a variable holding a reference to an object of class Symbol.
s = "a string"
#=> "a string"
s.class
#=> String
y = s.respond_to? z
#=> true
respond_to? is a method which asks an object whether it "recognizes" a
method, and the argument to respond_to? should be a symbol which is "the
same" (I'm deliberately not being precise here - symbols were confusing to
me when I first started using Ruby) as the method you want to see if the
object will recognize, which here is named include?. In other words, is
include? an instance method of the object. So this tells us that include? is
a method which "a string" will respond to. Let's try it.
s.include? "st"
#=> true
s.include? "ts"
#=> false
For more details, we can look up this method here (and others might be able
to explain how to use installed Ruby to get information on methods).
http://ruby-doc.org/
-> 1.8.6 Core
select class String
http://ruby-doc.org/core/classes/String.html
find the include? method
http://ruby-doc.org/core/classes/String.html#M000811
str.include? other_str => true or false
str.include? fixnum => true or false
Returns true if str contains the given string or character.
"hello".include? "lo" #=> true
"hello".include? "ol" #=> false
"hello".include? ?h #=> true