Forum: Ruby one-line function definitions

Ce5990549a8185ad45643be2cfbff9b5?d=identicon&s=25 Mike Steiner (Guest)
on 2007-06-18 19:23
(Received via mailing list)
I'm converting some code from Python to Ruby and can't figure out the
syntax
for one-line function definitions in Ruby. For example, what would this
line
be in Ruby?

def GetAnthologyTitle   ( data , prefix ): return GetField ( data ,
prefix ,
'ANTHOLOGY-TITLE'      )

Mike Steiner
7b4707f974af261f71943e1f2046c9ee?d=identicon&s=25 SonOfLilit (Guest)
on 2007-06-18 19:31
(Received via mailing list)
Transliterated (counting cultural differences), it owuld become:

def antology_title (data, prefix); get_field(data, prefix,
'ANTHOLOGY-TITLE'); end

But more importantly, it probably wouldn't exist, since ruby culture
sees explicit getters/setters as repeating yourself and finds
meta-solutions, a-la attr_accessor or def_delegator.

Aur
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-06-18 19:40
(Received via mailing list)
On 6/18/07, Mike Steiner <mikejaystei...@gmail.com> wrote:
> I'm converting some code from Python to Ruby and can't figure out the syntax
> for one-line function definitions in Ruby. For example, what would this line
> be in Ruby?
>
> def GetAnthologyTitle   ( data , prefix ): return GetField ( data , prefix ,
> 'ANTHOLOGY-TITLE'      )

def foo(x) x*x end
def bar(x) x-102 end
def jim(x) y=foo(x); bar(y) end
p jim(12)
#=> 42
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.