Ruby Document

Thanks Ts. Still not sure. Please read below:

  • class Unlock < ::Net::NetPrivate::HTTPRequest
    Q: what is the :: before Net mean?

This mean that Net is a top level constant
Can I omit this ::? Is it valid to omit it? or it has a “namespace” impact?
When should I use :: at top level?

  • define_http_method_interface :PropFind, true, true
    Q: what is the : before PropFind mean?

To say that it’s a symbol
What is a symbol? what is the difference between symbol and variable? Can
you tell me where in the document I can find the usage of symbole and :name
notation?

  • def HTTP.get( addr, path, port = nil )
    Q: difference between HTTP.get and HTTP#get? I vaguely know there are a
    notation of class method and instance method, but which is which? and

HTTP::get is a class method
It seems HTTP#get is a class method? or if not? what is the symbol # mean,
in method definition?

Tks.
Shannon

···

STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail

Can I omit this ::? Is it valid to omit it? or it has a "namespace" impact?

yes, it has an impact. For example

pigeon% cat b.rb
#!/usr/bin/ruby
class A; end

p A == ::A, A

module B
   p A == ::A, A

   class A
   end

   p A == ::A, A
end
pigeon%

pigeon% b.rb
true
A
true
A
false
B::A
pigeon%

For the 2 first test, A and ::A make reference to the same class

In the last test
   A make reference to the class B::A
   ::A make reference to the toplevel class A

When should I use :: at top level?

useless at toplevel :slight_smile:

What is a symbol? what is the difference between symbol and variable?

a symbol is an Object, like an Integer, Array, ...

pigeon% ruby -e 'p :name.class; p 12.class'
Symbol
Fixnum
pigeon%

It seems HTTP#get is a class method? or if not? what is the symbol # mean,
in method definition?

This is just a convention

  HTTP#method is the convention for an instance method
  HTTP::method is the convention for a class method

For example

   class HTTP
      def HTTP.get(a, b)
      end

      def gets(d)
      end
   end

HTTP::get is a class method
HTTP#gets is an instance method

Guy Decoux

“Shannon Fang” xrfang@hotmail.com writes:

Can I omit this ::? Is it valid to omit it? or it has a “namespace”
impact? When should I use :: at top level?

If you’re not inside any namespace, then Net::XX and ::Net::XX means
the same thing. However, if you are inside another namespace, let say
you are inside module A, then Net::XX really means A::Net::XX if such
thing is valid, or Net::XX if there is no A::Net::X. Because of this
ambiguity, you can specify ::Net::X as the absolute namespace to
choose.

What is a symbol? what is the difference between symbol and variable?

If you’re familiar with C, symbol is enum on steroid. Its primary
usage is as a constant for user program.

action=:sleep
if action == :sleep then … end
:sleep.to_s ==> “sleep”

Can you tell me where in the document I can find the usage of symbole
and :name notation?

The section “Ruby Language” in the pickaxe book is a good reference
that you may be looking for.

  • def HTTP.get( addr, path, port = nil )
    Q: difference between HTTP.get and HTTP#get? I vaguely know there are a
    notation of class method and instance method, but which is which? and

HTTP::get is a class method
It seems HTTP#get is a class method? or if not? what is the symbol #
mean, in method definition?

HTTP.get —> get is a class/module method of HTTP
HTTP#get —> get is an instance method of class HTTP (get is a method
of an object of type HTTP)

YS.

Hi Yohanes,

If you’re familiar with C, symbol is enum on steroid. Its primary
usage is as a constant for user program.

action=:sleep
if action == :sleep then … end
:sleep.to_s ==> “sleep”

I only know ansi C. I can’t say I know ansi C++, because it is way out
of control. To implement your code I will write,

action=“sleep”
if action==“sleep” then … end

Still very very confused, is symobol closer to a variable or closer to
the value of the variable? I don’t know why :sleep.to_s is “sleep”. What
is the relationship between symbol and string??

HTTP.get —> get is a class/module method of HTTP
HTTP#get —> get is an instance method of class HTTP (get is a method
of an object of type HTTP)
The above is quite clear, for example
class HTTP
def HTTP.get1
end
def HTTP#get2
end
def get3
end
end

Then, get1 is class method, get2 is instance method. How about get3???

Shannon

···

On Sun, 1 Dec 2002 05:37:04 +0900 Yohanes Santoso ysantoso@jenny-gnome.dyndns.org wrote:

Shannon Fang wrote:

I only know ansi C. I can’t say I know ansi C++, because it is way out
of control. To implement your code I will write,

action=“sleep”
if action==“sleep” then … end

A string comparition is both overkill for this kind of thing and posed
for the dangers inherent in mutable strings.

Still very very confused, is symobol closer to a variable or closer to
the value of the variable? I don’t know why :sleep.to_s is “sleep”. What
is the relationship between symbol and string??

As I understand it, a Symbol is just an internalized, immutable String.
Internalized as in stored in a big central place controlled by Ruby
itself. Immutable as in can’t be modified, never ever.

Imagine a big index of words which are the symbols. They are entered,
but never erased, from that big book of symbols. Since they are
internalized, you have less storage overhead. Each symbol merely
translates to a lookup in the big book, whereas both your ‘action’ and
‘“sleep”’ string would take up 4 spaces: the two String objects and each
of their underlying char* structures.

Symbols aren’t really closer to neither the variable nor the value of
the variable. A variable has a name. A method has a name. A class has a
name. And when you want to refer to the name itself (and not what the
name refers to), you can either waste resources with some Strings, or
use the unique Symbol for that name.

class HTTP
def HTTP.get1
end
def HTTP#get2
end
def get3
end
end

Then, get1 is class method,

Correct.

get2 is instance method.

No, it’s a parse error. However, it is a common way of denoting an
instance method, first seen in the PragProg ProgRuby book, AFAIK.

How about get3???

Now that is an instance method. But in plain text, we’d rather write
HTTP#get3 for brevity, since HTTP.get would be misleading and “get3 of
class HTTP” would bore the daylights out of us.

···


([ 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)

Try doing “ri Symbol”, or looking up Symbol in the Pickaxe (they’re
the same).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.12.01 at 09.43.02

···

On Sun, 1 Dec 2002 05:55:18 +0900, Shannon Fang wrote:

Still very very confused, is symobol closer to a variable or
closer to the value of the variable? I don’t know why :sleep.to_s
is “sleep”. What is the relationship between symbol and string??