# File::open and File.open

**URL:** <https://www.ruby-forum.com/t/file-open-and-file-open/73336>\
**Category:** Ruby\
**Created:** [October 23, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336 "2006-10-23T22:17:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 23, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/1 "2006-10-23T22:17:30Z")

</div>

What exactly is the difference between these? :: is a class method while  
. is an instance method??? If that is so, could someone clarify this  
with more detail? I use File.open, but File::open works too. However  
sometimes, this is not the case which adds to my confusion 🙂 For  
example, I can only do Digest::MD5.new() and not Digest.MD5.new() why is  
that?

I don’t fully understand OO programming… perhaps this is my problem.

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 23, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/2 "2006-10-23T22:25:55Z")

</div>

This is just Ruby syntax. It allows both :: and . as operators to  
class-level methods and variables.

File::open and File.open are the exact same thing: a class method. Were  
it  
an instance method, you would have to do:

file = File.new  
file.open

aka, create an instance of the object, then call the function. For  
confusions sake, this is the same as:

file = File::new  
file.open

Jason

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 23, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/3 "2006-10-23T22:32:07Z")

</div>

Brad T. wrote:

> What exactly is the difference between these? :: is a class method while  
> is an instance method??? If that is so, could someone clarify this  
> with more detail? I use File.open, but File::open works too.

The same method (a class method) is called. It is just a syntactic  
difference. So these are the same

File.open(“foo”) …  
File::open(“foo”) …

> However  
> sometimes, this is not the case which adds to my confusion 🙂 For  
> example, I can only do Digest::MD5.new() and not Digest.MD5.new() why is  
> that?

Because the “.” cannot be used for constant lookup:

irb(main):005:0\> File.NONBLOCK  
NoMethodError: undefined method `NONBLOCK’ for File:Class  
from (irb):5  
from :0  
irb(main):006:0\> File::NONBLOCK  
=\> 16384

“.” is reserved for method invocations.

> I don’t fully understand OO programming… perhaps this is my problem.

Nah, I think it is rather a minor ambiguity in the language that causes  
confusion. Usually people disambiguate it by using “.” for methods and  
“::” for constants only although they could use it for class methods as  
well.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 23, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/4 "2006-10-23T22:46:49Z")

</div>

Robert K. schrieb:

> ```
> from :0
> 
> ```
> 
> well.  
> I was bitten by it more than once  
> is there more to it … i mean ambiguity is usually not what one wants  
> to have in the language (at least not in the computer language 🙂

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 23, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/5 "2006-10-23T22:36:03Z")

</div>

Thank you Robert and Jason. I understand that much better now!

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [October 24, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/file-open-and-file-open/73336/6 "2006-10-24T23:35:13Z")

</div>

On 10/23/06, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is just Ruby syntax. It allows both :: and . as operators to  
> class-level methods and variables.
> 
> File::open and File.open are the exact same thing: a class method. Were it  
> an instance method, you would have to do:
> 
> file = File.new  
> file.open

And when talking about instance methods one sometimes uses

classname#method name

Since this is what ri understands when you ask for documentation:

$ri to\_s  
More than one method matched your request. You can refine  
your search by asking for information on one of:

```
 Method#to_s, Vector#to_s, Struct#to_s, Time#to_s, CGI::Cookie#to_s,
 Array#to_s, Matrix#to_s, MatchData#to_s, FalseClass#to_s,
 Pathname#to_s, Pathname#to_str, Rational#to_s, Hash#to_s,
 UnboundMethod#to_s, TrueClass#to_s, Module#to_s, Complex#to_s,
 Proc#to_s, Symbol#to_s, Symbol#to_sym, Exception#to_s,
 Exception#to_str, Bignum#to_s, Object#to_s, NilClass#to_s,
 Range#to_s, Date#to_s, NameError#to_s, Fixnum#to_s, Fixnum#to_sym,
 Float#to_s, String#to_s, String#to_str, String#to_sym, Regexp#to_s,
 Benchmark::Tms#to_s, URI::MailTo#to_s, URI::FTP#to_s,
 URI::Generic#to_s, Process::Status#to_s, Enumerable#to_set

```

## $ri Object#to\_s ------------------------------------------------------------ Object#to\_s obj.to\_s =\> string

```
 Returns a string representing _obj_. The default +to_s+ prints the
 object's class and an encoding of the object id. As a special case,
 the top-level object that is the initial execution context of Ruby
 programs returns ``main.''

```

And, by the way, the open method in question is really a class method  
of IO rather than File, the File class inherits it.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)
