# Syntax Opinions

**URL:** <https://www.ruby-forum.com/t/syntax-opinions/128558>\
**Category:** Ruby\
**Created:** [February 8, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/syntax-opinions/128558 "2008-02-08T21:30:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 8, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/syntax-opinions/128558/1 "2008-02-08T21:30:53Z")

</div>

Hello,

I am working on a “framework glue”, as in this framework won’t make you  
lose weight or cure the common cold, it’ll just combine the power of  
MVC, Sinatra, and DM. However, I’m stuck on what syntax to use for  
defining methods in the controller. Since each method will have an HTTP  
method, there has to be some way to choose this. Below are my two  
options:

1. Enclose the methods in a block with the same name as the HTTP method.  
get do  
def post  
@post = …  
end  
end

2. My personal favourite which needs no explanation…  
def @get.post  
@post = …  
end

What do you guys think?

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 8, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/syntax-opinions/128558/2 "2008-02-08T22:06:13Z")

</div>

On Fri, 08 Feb 2008 15:30:52 -0500, Michael B. wrote:

> 1. Enclose the methods in a block with the same name as the HTTP method.  
> get do  
> def post  
> @post = …  
> end  
> end

verbose

> 1. My personal favourite which needs no explanation…  
> def @get.post  
> @post = …  
> end
> 
> What do you guys think?

Seems better, but why not

def get.post ?  
@post…  
end

You just need to provide self.get (and self.post/put/delete) as an  
accessor on the base class, which is also useful for setting up the  
value  
conditionally. But probably you meant this.

There is also one more option, which is more hackish (and not thread  
safe) which would be:

class C  
get  
def foo… end  
def bar… end  
post  
def foo… end  
def quux … end  
end

Which IMO is way better than the rest, easily allows you to avoid  
specifying anything on the default GET, but suggests grouping of  
similar  
HTTP versbs. Dummy implementation:

> > end  
> > POST=[]  
> > GET=[]  
> > end  
> > =\> []
> 
> > class D \< C  
> > get  
> > def foo() ‘foo’ end  
> > def bar() ‘bar’ end  
> > post  
> > def quux() ‘quux’ end  
> > def foo() ‘foo post’ end  
> > end  
> > =\> nil
> 
> > D::POST  
> > =\> [:quux, :foo]
> 
> > D::GET  
> > =\> [:foo, :bar]

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 10, 2008, 9:17am UTC](https://www.ruby-forum.com/t/syntax-opinions/128558/3 "2008-02-10T09:17:52Z")

</div>

Michael B. wrote:

> 1. Enclose the methods in a block with the same name as the HTTP method.
> 
> What do you guys think?

Everyone has their own conventions that fit their brain best. Mine is  
def edit;end #get  
def edit!;end #post

So it’s not so important which convention you choose (i.e. there is no  
objectively “best” one), just that you be comfortable with it and stick  
with it for the lifetime of the project.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [February 10, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/syntax-opinions/128558/4 "2008-02-10T17:10:07Z")

</div>

Why not simply write

def post\_book

# create

end

def get\_book

# read

end

def put\_book

# update

end

Or use a macro like:

get(:book) do  
end

put(:book) do  
end

2008/2/10, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
