# Snippet request: Ruby Web Server written in under an hour

**URL:** <https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516>\
**Category:** ruby-talk\
**Created:** [28 October 2002 04:48 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516 "2002-10-28T04:48:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip5](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Phlip5](https://rubytalk.org/u/Phlip5)\
**Post date:** [28 October 2002 04:48 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/1 "2002-10-28T04:48:46Z")

</div>

Rubies:

Plenty of blogs report D. Thomas and crew immersed some of you and let you  
write a Web server in under an hour.

Nobody has left a result of this experience on the 'net. Could someone post  
it here, please?

I’m not asking because I’m lazy to learn Ruby, I’m just lazy to learn how  
Web servers work. 😉

> **···**
>
> –  
> Phlip  
> [http://www.greencheese.org/HoneySatellite](http://www.greencheese.org/HoneySatellite)  
> – /Hay sordos, pero no hay mas sordo que el que no quiere oir/ –

---

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://rubytalk.org/u/Dave_Thomas)\
**Post date:** [28 October 2002 12:41 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/2 "2002-10-28T12:41:08Z")

</div>

Phlip [phlipcpp@yahoo.com](mailto:phlipcpp@yahoo.com) writes:

> Plenty of blogs report D. Thomas and crew immersed some of you and let you  
> write a Web server in under an hour.
> 
> Nobody has left a result of this experience on the 'net. Could someone post  
> it here, please?

We do this at our full-day Ruby tutorials, although we’ve never been  
generous enough to give folks a full hour…🙂 (and, yes, most folks get  
something working in the time). However, I’m not sure anyone has ever  
posted what they’ve written.

Dave

---

<div class="post-metadata">

**Author:** ![Joseph\_McDonald](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Joseph\_McDonald](https://rubytalk.org/u/Joseph_McDonald)\
**Post date:** [28 October 2002 18:15 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/3 "2002-10-28T18:15:19Z")

</div>

> Plenty of blogs report D. Thomas and crew immersed some of you and let you  
> write a Web server in under an hour.

> Nobody has left a result of this experience on the 'net. Could someone post  
> it here, please?

> I’m not asking because I’m lazy to learn Ruby, I’m just lazy to learn how  
> Web servers work. 😉

Have you seen:  
[http://www.xs4all.nl/~hipster/lib/ruby/httpd](http://www.xs4all.nl/~hipster/lib/ruby/httpd)

The newer version:  
[http://www.xs4all.nl/~hipster/lib/ruby/httpd-1.6.1.6](http://www.xs4all.nl/~hipster/lib/ruby/httpd-1.6.1.6)

also:  
[http://www.ruby-lang.org/en/raa-list.rhtml?id=542](http://www.ruby-lang.org/en/raa-list.rhtml?id=542)  
[http://www.ruby-lang.org/en/raa-list.rhtml?id=631](http://www.ruby-lang.org/en/raa-list.rhtml?id=631)  
[http://www.ruby-lang.org/en/raa-list.rhtml?id=54](http://www.ruby-lang.org/en/raa-list.rhtml?id=54)

regards,  
-joe

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [28 October 2002 13:28 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/4 "2002-10-28T13:28:33Z")

</div>

Is it total immersion? Going from “WTH is Ruby?” to writing a web server  
in under one hour is _very_ impressive.

> **···**
>
> On Mon, Oct 28, 2002 at 09:41:08PM +0900, Dave Thomas wrote:
> 
> > Phlip [phlipcpp@yahoo.com](mailto:phlipcpp@yahoo.com) writes:
> > 
> > > Plenty of blogs report D. Thomas and crew immersed some of you and let you  
> > > write a Web server in under an hour.
> > > 
> > > Nobody has left a result of this experience on the 'net. Could someone post  
> > > it here, please?
> > 
> > We do this at our full-day Ruby tutorials, although we’ve never been  
> > generous enough to give folks a full hour…🙂 (and, yes, most folks get  
> > something working in the time). However, I’m not sure anyone has ever  
> > posted what they’ve written.
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> “You, sir, are nothing but a pathetically lame salesdroid!  
> I fart in your general direction!”  
> – Randseed on #Linux

---

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://rubytalk.org/u/Dave_Thomas)\
**Post date:** [28 October 2002 13:39 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/5 "2002-10-28T13:39:09Z")

</div>

Mauricio Fernández [batsman.geo@yahoo.com](mailto:batsman.geo@yahoo.com) writes:

> > We do this at our full-day Ruby tutorials, although we’ve never been  
> > generous enough to give folks a full hour…🙂 (and, yes, most folks get  
> > something working in the time). However, I’m not sure anyone has ever  
> > posted what they’ve written.
> 
> Is it total immersion? Going from “WTH is Ruby?” to writing a web server  
> in under one hour is _very_ impressive.

It’s a six hour course, with lots of exercises. We normally ask people  
to pair.

Dave

---

<div class="post-metadata">

**Author:** ![Phlip5](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Phlip5](https://rubytalk.org/u/Phlip5)\
**Post date:** [28 October 2002 14:29 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/6 "2002-10-28T14:29:40Z")

</div>

Dave Thomas wrote:

> It’s a six hour course, with lots of exercises. We normally ask people  
> to pair.

Hi, Dave.

You of all people should have the snippet…

> **···**
>
> –  
> Phlip  
> [greencheese.org](http://www.greencheese.org/SkeletonCrew)  
> – Why is the “Cheesy Horror Movie Channel” called “SciFi”?? –

---

<div class="post-metadata">

**Author:** ![Phlip5](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Phlip5](https://rubytalk.org/u/Phlip5)\
**Post date:** [29 October 2002 05:10 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/7 "2002-10-29T05:10:49Z")

</div>

Rubies:

This would have taken me less than a wall-clock hour if I didn’t have a  
life:

#!/usr/bin/ruby

# a web server in Ruby

require ‘socket’

$mimeTypes = { ‘jpg’ =\> ‘image/jgp’, ‘gif’ =\> ‘image/gif’, ‘png’ =\>  
‘image/png’,  
‘html’ =\> ‘text/html’, ‘pdf’ =\> ‘application/pdf’ }

$response = {} # TODO make me a map…

$response[200] =  
’’'HTTP/1.0 200 Okay  
Server: ws30  
Content-type: %s

%s  
’’’

$response[301] =  
"""HTTP/1.0 301 Moved  
Server: ws30  
Content-type: text/plain  
Location: %s

moved%s  
"""

$response[404] =  
’’'HTTP/1.0 404 Not Found  
Server: ws30  
Content-type: text/plain

%s not found%s  
’’’

def serverSocket host, port

# TODO make these real arguments…

```
s = TCPServer.new('localhost', port)
return s

```

end

def listen s  
connection, client = s.accept()  
return connection  
end

def getRequest stream  
method = nil

```
while 1 do
    line = stream.readline()
    if line.strip().length() == 0 then
        break
    elsif ! method
        method, uri, protocol = line.split()
    end
end

return uri

```

end

def listDirectory uri  
return 'directory none of your business’  
end

def getFile path  
f = open( path )  
return f.read() # TODO begin/rescue here  
end

def getContent uri  
print ‘fetching:’, uri

```
begin
    path = '.' + uri 
    if File.file?( path ) then
        return [200, getMime( uri ), getFile( path )]
    end
    if File.directory?( path ) then
        if( uri[uri.length() - 1].chr == '/' ) then
            return [200, 'text/html', listDirectory( uri )]
        else
            return [301, uri + '/']
        end
    else
        return [404, uri]
    end

```

# TODO actually stringify the error 😉

```
rescue
    return [404, 'some dumb IO error']
end

```

end

def getMime uri  
type = $mimeTypes[uri.split(’.’)[-1]]  
type = ‘text/plain’ if type == nil  
return type  
end

def sendResponse stream, content  
$responseType = $response[content[0]]  
content.push("")  
response = sprintf($responseType, content[1], content[2] )  
stream.write( response )  
end

if **FILE** == $0 then

```
server = serverSocket( 'localhost', 8080 )

begin
    while 1 do
        stream = listen ( server )
        sendResponse( stream, getContent( getRequest( stream ) ) )
        stream.close()
    end
rescue
    print 'shutting down...'
end

server.close()

```

end

> **···**
>
> –  
> Phlip  
> [http://flea.sourceforge.net](http://flea.sourceforge.net)  
> – Got in trouble at StarBucks. I tried to order  
> "A double latte mocha and a body piercing." –

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [29 October 2002 14:38 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/8 "2002-10-29T14:38:57Z")

</div>

Hi –

> Rubies:

Horse and rider are one? 🙂

> def listen s  
> connection, client = s.accept()  
> return connection  
> end

I have a style-related question: I noticed that you put () after all  
method names, and I was wondering what the origin/purpose/whatever of  
that was. I’m neither trying to criticize it, nor likely to adopt  
it myself 🙂 Just wondering.

David

> **···**
>
> On Tue, 29 Oct 2002, Phlip wrote:
> 
> –  
> David Alan Black  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![Thomas\_A\_Reilly](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@Thomas\_A\_Reilly](https://rubytalk.org/u/Thomas_A_Reilly)\
**Post date:** [29 October 2002 19:05 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/9 "2002-10-29T19:05:38Z")

</div>

I needed to use FTP using the Zaurus PDA. I can hardly believe how easy it  
was to set up using Ruby.

The code follows if anyone else is interested.

require "net/ftp"  
t = Net::FTP.new(host=nil,user=nil,passwd=nil)  
t.connect(‘192.168.130.101’,port=4242)  
t.login(user=‘root’, passwd=’ ‘)  
files = t.chdir(’/mnt/card/Documents/application/octet-stream’)  
print "Transferring Program"  
t.puttextfile(“[asz.py](http://asz.py)”,“[asz.py](http://asz.py)”,callback=nil)  
print "Transferring Index"  
t.puttextfile(“asindex.pyz”,“asindex.pyz”,callback=nil)  
print "Transferring Data"  
t.puttextfile(“asksam.pyz”,“asksam.pyz”,callback=nil)  
t.close

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [29 October 2002 15:46 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/10 "2002-10-29T15:46:46Z")

</div>

I do the same thing. It makes it clear that I’m calling a method and  
not retrieving an attribute. I suppose it comes from my C++ background:

class Foo  
{  
void foo()  
int bar;  
};

Foo f;  
f.foo();  
int x = f.bar;

Paul

> **···**
>
> On Tue, Oct 29, 2002 at 11:38:57PM +0900, [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:
> 
> > I have a style-related question: I noticed that you put () after all  
> > method names, and I was wondering what the origin/purpose/whatever of  
> > that was. I’m neither trying to criticize it, nor likely to adopt  
> > it myself 🙂 Just wondering.

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [29 October 2002 16:11 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/11 "2002-10-29T16:11:34Z")

</div>

[dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:

> I have a style-related question: I noticed that you put () after all  
> method names, and I was wondering what the origin/purpose/whatever of  
> that was. I’m neither trying to criticize it, nor likely to adopt  
> it myself 🙂 Just wondering.

Hi David,

This is just my personal style. When I write network simulations (not  
Ruby code fragments), I never put () for methods that are defined simply  
using “attr\_reader”, but other methods I usually put () to remind  
me that I am “doing some real action” here. Well, in Ruby () is really  
optional for methods without arguments, so I just take this advantage to  
mimic the C’s member data access vs function call distinction. On the  
other hand, for a method call with a single argument of type block, I  
don’t put () either, because the {} is enough as a reminder to me that  
“some real action is going on here”.

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [29 October 2002 16:11 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/12 "2002-10-29T16:11:43Z")

</div>

Hi –

> **···**
>
> On Wed, 30 Oct 2002, Paul Brannan wrote:
> 
> > On Tue, Oct 29, 2002 at 11:38:57PM +0900, [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:
> > 
> > > I have a style-related question: I noticed that you put () after all  
> > > method names, and I was wondering what the origin/purpose/whatever of  
> > > that was. I’m neither trying to criticize it, nor likely to adopt  
> > > it myself 🙂 Just wondering.
> > 
> > I do the same thing. It makes it clear that I’m calling a method and  
> > not retrieving an attribute. I suppose it comes from my C++ background:
> > 
> > class Foo  
> > {  
> > void foo()  
> > int bar;  
> > };
> > 
> > Foo f;  
> > f.foo();  
> > int x = f.bar;
> 
> But since attributes are retrieved by method calls in Ruby, that  
> distinction doesn’t hold. Or, to put it another way, since all cases  
> of obj.message in Ruby are the foo() case, the () doesn’t actually add  
> any information or tell us anything. I guess that’s why it leapt off  
> the screen at me a bit.
> 
> David
> 
> –  
> David Alan Black  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![Bulat\_Ziganshin](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bulat\_Ziganshin](https://rubytalk.org/u/Bulat_Ziganshin)\
**Post date:** [29 October 2002 16:16 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/13 "2002-10-29T16:16:16Z")

</div>

Hello William,

Tuesday, October 29, 2002, 7:11:34 PM, you wrote:

> This is just my personal style. When I write network simulations (not  
> Ruby code fragments), I never put () for methods that are defined simply  
> using “attr\_reader”, but other methods I usually put () to remind  
> me that I am “doing some real action” here.

i think real difference that you you want to highlight is between pure  
functions and functions which have side-effect

> **···**
>
> –  
> Best regards,  
> Bulat [mailto:bulatz@integ.ru](mailto:bulatz@integ.ru)

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [29 October 2002 16:16 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/14 "2002-10-29T16:16:26Z")

</div>

Hi –

> **···**
>
> On Wed, 30 Oct 2002, William Djaja Tjokroaminata wrote:
> 
> > [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:
> > 
> > > I have a style-related question: I noticed that you put () after all  
> > > method names, and I was wondering what the origin/purpose/whatever of  
> > > that was. I’m neither trying to criticize it, nor likely to adopt  
> > > it myself 🙂 Just wondering.
> > 
> > Hi David,
> > 
> > This is just my personal style. When I write network simulations (not  
> > Ruby code fragments), I never put () for methods that are defined simply  
> > using “attr\_reader”, but other methods I usually put () to remind  
> > me that I am “doing some real action” here. Well, in Ruby () is really  
> > optional for methods without arguments, so I just take this advantage to  
> > mimic the C’s member data access vs function call distinction. On the  
> > other hand, for a method call with a single argument of type block, I  
> > don’t put () either, because the {} is enough as a reminder to me that  
> > “some real action is going on here”.
> 
> Doesn’t that mean that you always have to know whether something was  
> defined with attr\_reader? That seems like a kind of arbitrary thing  
> – it could always be redefined. Also, what if you’re using a library  
> package?
> 
> David
> 
> –  
> David Alan Black  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [30 October 2002 04:49 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/15 "2002-10-30T04:49:29Z")

</div>

Server)

> > I have a style-related question: I noticed that you put () after all  
> > method names, and I was wondering what the origin/purpose/whatever of  
> > that was. I’m neither trying to criticize it, nor likely to adopt  
> > it myself 🙂 Just wondering.
> 
> I do the same thing. It makes it clear that I’m calling a method and  
> not retrieving an attribute. I suppose it comes from my C++ background:

That’s very interesting. I do the exact opposite  
for the exact opposite reason. 🙂

I.e., I don’t want the code to show whether I’m  
calling a “regular” method or just an accessor.  
This seems in line with Meyer’s Uniform Access  
Principle, IIRC.

Hal

> **···**
>
> ----- Original Message -----  
> From: “Paul Brannan” [pbrannan@atdesk.com](mailto:pbrannan@atdesk.com)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Tuesday, October 29, 2002 9:46 AM  
> Subject: Re: method-call style (was Re: Snippet: Tiny Featureless Ruby Web
> 
> > On Tue, Oct 29, 2002 at 11:38:57PM +0900, [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:

---

<div class="post-metadata">

**Author:** ![Phlip5](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Phlip5](https://rubytalk.org/u/Phlip5)\
**Post date:** [30 October 2002 15:17 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/16 "2002-10-30T15:17:47Z")

</div>

Paul Brannan wrote:

> I do the same thing. It makes it clear that I’m calling a method and  
> not retrieving an attribute. I suppose it comes from my C++ background:
> 
> class Foo  
> {  
> void foo()  
> int bar;  
> };

Programming is about communicating with humans first, a computer second.

(I’m aware my snippet hardly exemplifies this lofty ideal…)

I do it to say “calling a function here”.

Props to Matz for not making me always do it. For example,

```
    puts indescribable.public_methods.sort()

```

Drop the middle () because it’s redundant.

And where would [http://flea.sf.net](http://flea.sf.net) without the call chain?

root.move.move.move.move.move.color(White).longer(2).  
push.link(ax).pop.right.  
push.link(ax).pop.right.  
push.link(ax).pop.right.  
push.link(ax).pop.right.  
push.link(ax).pop.right.  
push.link(ax).pop.right

Those are procedural function calls, but I want them to look like commands  
in a miniature declarative language. And the ones that need arguments get  
() for parsinomy.

> **···**
>
> –  
> Phlip  
> [greencheese.org](http://www.greencheese.org/YaAw)  
> – Wanted: Marriage counselor who also keeps pet rats –

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [29 October 2002 18:56 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/17 "2002-10-29T18:56:04Z")

</div>

In Ruby, attributes are method calls. However, they are a special type  
of method call. Attributes are guaranteed to never modify the object.  
And if the object isn’t modified, then the attribute should remain the  
same. I think the distinction is supported by the semantics of  
attributes, if not by the language itself.

Paul

> **···**
>
> On Wed, Oct 30, 2002 at 01:11:43AM +0900, [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:
> 
> > But since attributes are retrieved by method calls in Ruby, that  
> > distinction doesn’t hold. Or, to put it another way, since all cases  
> > of obj.message in Ruby are the foo() case, the () doesn’t actually add  
> > any information or tell us anything. I guess that’s why it leapt off  
> > the screen at me a bit.

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [30 October 2002 15:31 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/18 "2002-10-30T15:31:24Z")

</div>

Hi –

> **···**
>
> On Thu, 31 Oct 2002, Phlip wrote:
> 
> > Paul Brannan wrote:
> > 
> > > I do the same thing. It makes it clear that I’m calling a method and  
> > > not retrieving an attribute. I suppose it comes from my C++ background:
> > > 
> > > class Foo  
> > > {  
> > > void foo()  
> > > int bar;  
> > > };
> > 
> > Programming is about communicating with humans first, a computer second.
> > 
> > (I’m aware my snippet hardly exemplifies this lofty ideal…)
> > 
> > I do it to say “calling a function here”.
> > 
> > Props to Matz for not making me always do it. For example,
> > 
> > ```
> > puts indescribable.public_methods.sort()
> > 
> > ```
> > 
> > Drop the middle () because it’s redundant.
> 
> True… but the last () is too, since public\_methods.sort can only be  
> a method call. I guess it’s a question of what the eye is used to.  
> As you point out, we all get to choose, without any loss of clarity  
> 🙂
> 
> David
> 
> –  
> David Alan Black  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [30 October 2002 15:37 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/19 "2002-10-30T15:37:53Z")

</div>

Hi Bulat,

Yes, I agree with you; I think you have made it clearer, similar to Paul’s  
post.

Regards,

Bill

> **···**
>
> Bulat Ziganshin [bulatz@integ.ru](mailto:bulatz@integ.ru) wrote:
> 
> > > This is just my personal style. When I write network simulations (not  
> > > Ruby code fragments), I never put () for methods that are defined simply  
> > > using “attr\_reader”, but other methods I usually put () to remind  
> > > me that I am “doing some real action” here.
> 
> > i think real difference that you you want to highlight is between pure  
> > functions and functions which have side-effect

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [30 October 2002 15:57 UTC](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516/20 "2002-10-30T15:57:53Z")

</div>

[dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:

> Doesn’t that mean that you always have to know whether something was  
> defined with attr\_reader? That seems like a kind of arbitrary thing  
> – it could always be redefined. Also, what if you’re using a library  
> package?

Hi David,

I think you have a very good point here.

In my case, in some sense I develop the object model hierarchy from  
scratch, so I do know all the properties of the simulation objects. For  
other people, in my manual for each class I distinguish between  
“attributes” and “methods”. Attributes are things that are implemented  
using the “@var”. This is done so that people who want to inherit from  
a class can access the “@var” directly and do not have to use  
“self.var”. Well, now people who know OO well may tell me whether it is  
better for a derived class to access the “@var” directly or instead to use  
“self.var”.

If I’m using a library package, on the other hand, then it will be really  
subjective. For example, I may write array.length but array.sort(). It  
is just a personal habit to treat “length” as if it is some data stored in  
memory but “sort” as resulting in some processing. Well, as already  
pointed out by someone else, Meyer’s uniform access principle says that we  
don’t need to know how “length” is really implemented; so this is really  
just a personal style.

Regards,

Bill

[Next page](https://rubytalk.org/t/snippet-request-ruby-web-server-written-in-under-an-hour/2516.md?page=2)
