[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Function def at end of scripts

Reasoned Insanity

12/12/2008 5:06:00 PM

Hi,

I am refactoring an ugly script. I would like it contained all in one
file so I was moving code to functions/classes and put them at the end
of the script for legibility. This didn't work. They are only available
if I put them at the beginning of the script before I need them. Is
there a way to declare them at the beginning so I can put them all at
the end of the file?

Thanks!
Eric
--
Posted via http://www.ruby-....

10 Answers

Sebastian Hungerecker

12/12/2008 5:30:00 PM

0

No Where wrote:
> Is there a way to declare [methods] at the beginning so I can put them
> all at the end of the file?

No. The fact that you can't use methods before their definition isn't because
of a shortcoming of the interpreter and thus can't be "fixed" by forward
declaration like in C. You can't use a method before its definition in ruby
because in ruby method definitions happen at run-time not at parse-time. This
needs to be so in order to allow method redefinition.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

The Higgs bozo

12/12/2008 6:08:00 PM

0

No Where wrote:
> I am refactoring an ugly script. I would like it contained all in one
> file so I was moving code to functions/classes and put them at the end
> of the script for legibility. This didn't work. They are only available
> if I put them at the beginning of the script before I need them. Is
> there a way to declare them at the beginning so I can put them all at
> the end of the file?

IIUC you want to do this,

do_stuff(4, 5)

def do_stuff(x, y)
p x ; p y
end

which won't fly. However code running in the top-level scope is only
suitable for the smallest of scripts anyway. I recommend:

def main
do_stuff(4, 5)
end

def do_stuff(x, y)
p x ; p y
end

main
--
Posted via http://www.ruby-....

Joel VanderWerf

12/12/2008 7:21:00 PM

0

No Where wrote:
> Hi,
>
> I am refactoring an ugly script. I would like it contained all in one
> file so I was moving code to functions/classes and put them at the end
> of the script for legibility. This didn't work. They are only available
> if I put them at the beginning of the script before I need them. Is
> there a way to declare them at the beginning so I can put them all at
> the end of the file?
>
> Thanks!
> Eric

The END {...} construct can be helpful:

END {
main
}

def main
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

ara.t.howard

12/13/2008 12:03:00 AM

0


On Dec 12, 2008, at 10:06 AM, No Where wrote:

> Hi,
>
> I am refactoring an ugly script. I would like it contained all in one
> file so I was moving code to functions/classes and put them at the end
> of the script for legibility. This didn't work. They are only
> available
> if I put them at the beginning of the script before I need them. Is
> there a way to declare them at the beginning so I can put them all at
> the end of the file?
>
> Thanks!
> Eric
> --
> Posted via http://www.ruby-....
>


stuff


BEGIN {
def stuff
end
}

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Einar Magnús Boson

12/13/2008 12:35:00 AM

0


On 13.12.2008, at 00:02 , ara.t.howard wrote:

>
> On Dec 12, 2008, at 10:06 AM, No Where wrote:
>
>> Hi,
>>
>> I am refactoring an ugly script. I would like it contained all in one
>> file so I was moving code to functions/classes and put them at the
>> end
>> of the script for legibility. This didn't work. They are only
>> available
>> if I put them at the beginning of the script before I need them. Is
>> there a way to declare them at the beginning so I can put them all at
>> the end of the file?
>>
>> Thanks!
>> Eric
>> --
>> Posted via http://www.ruby-....
>>
>
>
> stuff
>
>
> BEGIN {
> def stuff
> end
> }
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
>


#!/usr/bin/env ruby -wKU
END {
puts "http://www.java2s.com/Code/Ruby/Statement/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramis...
"
}

stuff

BEGIN {
def stuff
puts "who would've guessed?"
end
}
# >> who would've guessed?
# >> http://www.java2s.com/Code/Ruby/Statement/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramis...

This is cool. Thanks!
einarmagnus




Brian Candler

12/13/2008 8:53:00 AM

0

Or if you subscribe to the Camping school of application development:

eval DATA.read
say_hello

__END__
def say_hello
puts "hi"
end

Extra points if you run gsub transformations on DATA.read too.
--
Posted via http://www.ruby-....

Einar Magnús Boson

12/13/2008 5:34:00 PM

0


On 13.12.2008, at 09:11 , David A. Black wrote:

> This message is in MIME format. The first part should be readable =20=

> text,
> while the remaining parts are likely unreadable without MIME-aware =20=

> tools.
> Hi --
>
> On Sat, 13 Dec 2008, Einar Magn=FAs Boson wrote:
>
>>
>> On 13.12.2008, at 00:02 , ara.t.howard wrote:
>>
>>> On Dec 12, 2008, at 10:06 AM, No Where wrote:
>>>> Hi,
>>>> I am refactoring an ugly script. I would like it contained all in =20=

>>>> one
>>>> file so I was moving code to functions/classes and put them at =20
>>>> the end
>>>> of the script for legibility. This didn't work. They are only =20
>>>> available
>>>> if I put them at the beginning of the script before I need them. Is
>>>> there a way to declare them at the beginning so I can put them =20
>>>> all at
>>>> the end of the file?
>>>> Thanks!
>>>> Eric
>>>> --=20
>>>> Posted via http://www.ruby-....
>>> stuff
>>> BEGIN {
>>> def stuff
>>> end
>>> }
>>> a @ http://codeforp...
>>> --
>>> we can deny everything, except that we have the possibility of =20
>>> being better. simply reflect on that.
>>> h.h. the 14th dalai lama
>>
>>
>> #!/usr/bin/env ruby -wKU
>> END {
>> puts =
"http://www.java2s.com/Code/Ruby/Statement/Thecodeinablocklabeled...
ywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm=20
>> " }
>>
>> stuff
>>
>> BEGIN {
>> def stuff
>> puts "who would've guessed?"
>> end
>> }
>> # >> who would've guessed?
>> # >> =
http://www.java2s.com/Code/Ruby/Statement/Thecodeinablocklabeledw...
wordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm
>>
>> This is cool. Thanks!
>
> It can serve a purpose, but please have mercy on those of us who like
> our Ruby in order :-) It's really not that hard to get used to the
> principle that the code (including method definitions) is executed as
> it's encountered, and then you don't have to think about whether or
> not to flip it.
>
>
> David
>
> --=20
> Rails training from David A. Black and Ruby Power and Light:
> INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL
> See http://www.r... for details
> Coming in 2009: The Well-Grounded Rubyist (http://manning....)


I actually have a hard time envisioning a scenario where I would ever =20=

use this. Still good to know of. Looking into it made me find some =20
other weird stuff I had never come across before though, like the flip-=20=

flop operator that took me a while to figure out properly. That is =20
also something I don't see myself using though, it has a bit too =20
complicated semantics for my taste and feels like obfuscation just to =20=

avoid an extra line.


einarmagnus


Reasoned Insanity

12/15/2008 4:57:00 PM

0

The Higgs bozo wrote:
> def main
> do_stuff(4, 5)
> end
>
> def do_stuff(x, y)
> p x ; p y
> end
>
> main

Thanks to all for the replies and I'm glad I sparked a bit of
philosophizin too ;)

This seems like the simplest way to get what I wanted at the beginning
BUT, if it's an accepted methodology to do in order, then I will place
it at the end. I have been learning Ruby and one way I practice it is to
quit using other scripting languages to do do my simple scripting and
work with Ruby for a while. Repetition is about the only way it will
stick in the toolbox for me...

Eric
--
Posted via http://www.ruby-....

ara.t.howard

12/15/2008 5:21:00 PM

0


On Dec 13, 2008, at 2:11 AM, David A. Black wrote:

> It can serve a purpose, but please have mercy on those of us who like
> our Ruby in order :-) It's really not that hard to get used to the
> principle that the code (including method definitions) is executed as
> it's encountered, and then you don't have to think about whether or
> not to flip it.

i like it in order too - logical order that is:

class Controller
before_filter do
require_user
end

def action
end
end

class Model
has_many :children do
def extension
end
end
end


the reality is that logical order and organization is far more
important, consider reading a rails app 'in order' ;-) or having to
install your own 'run' hook at the end of every test suite. the
reality is that it's situational - if a script has 50 methods before
it gets around to the 'main' call any sane person might enjoy reading
the 'help' method first. this is an overriding principle of my
'main.rb' lib and i think most people would agree that reading
something like

http://codeforp...lib/ruby/tumblr/tumblr-0.0.1/...

is preferable than needing to dig through the source until the help
method happens to come along. same goes for associations in models
and filters in controllers, etc. also consider that a block in ruby
may or may not be executed in the same order that you read it

task do
good thing this doesn't run here
end

or the fact that helper methods aren't injected into a view in rails
until very late in the execution phase and are silo'd off precisely so
that we do NOT have to read them en route to understanding a view.

the order of code is always situational but i think that people who
read alot of code from many languages, not only ruby, will agree that
logical order is the one thing that should always be considered,
language limitations/conventions are always a secondary consideration
unless one programs exclusively in one language. i think this is
actually quite important since the evolution of ruby will introduce
compliers and other enhancements that will/are undoubtedly change the
way code is evaluated while logical constructs will remain stable.

so my thinking is that rubyists should *always* strive to present code
in a rough logical order so long as great deviations from the idioms
of the language are not made. this included organizing projects into
a good hierarchy, factoring out libs, using deferred execution
(lambda), and tools such as BEGIN|END|at_exit etc. by using all the
tools provided to support the cleanest logical presentation code
reading will be enhanced for even those without vast rubyfu.

my 2 cts.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




David A. Black

12/15/2008 6:39:00 PM

0

Hi --

On Tue, 16 Dec 2008, ara.t.howard wrote:

>
> On Dec 13, 2008, at 2:11 AM, David A. Black wrote:
>
>> It can serve a purpose, but please have mercy on those of us who like
>> our Ruby in order :-) It's really not that hard to get used to the
>> principle that the code (including method definitions) is executed as
>> it's encountered, and then you don't have to think about whether or
>> not to flip it.
>
> i like it in order too - logical order that is:
>
> class Controller
> before_filter do
> require_user
> end
>
> def action
> end
> end
>
> class Model
> has_many :children do
> def extension
> end
> end
> end
>
>
> the reality is that logical order and organization is far more important,
> consider reading a rails app 'in order' ;-) or having to install your own
> 'run' hook at the end of every test suite. the reality is that it's
> situational - if a script has 50 methods before it gets around to the 'main'
> call any sane person might enjoy reading the 'help' method first. this is an
> overriding principle of my 'main.rb' lib and i think most people would agree
> that reading something like

I'm not trying to make any big prounouncements or prescriptions about
the concept of order as a software engineering principle. I just
don't see code-flipping plus BEGIN {} as a general-usage good way to
organize code. There's nothing to extrapolate from this (e.g., about
ActiveRecord association semantics); I'm just talking (right or wrong)
about this one thing.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)