Function def at end of scripts

N

No Where

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
 
S

Sebastian Hungerecker

No said:
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
 
T

The Higgs bozo

No said:
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
 
J

Joel VanderWerf

No said:
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
 
A

ara.t.howard

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


stuff


BEGIN {
def stuff
end
}

a @ http://codeforpeople.com/
 
B

Brian Candler

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.
 
E

Einar Magnús Boson

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 --

"http://www.java2s.com/Code/Ruby/Statement/Thecodeinablocklabeledwiththeke=
ywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm=20 http://www.java2s.com/Code/Ruby/Statement/Thecodeinablocklabeledwiththekey=
wordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm

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.rubypal.com for details
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)


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
 
N

No Where

The said:
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
 
A

ara.t.howard

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://codeforpeople.com/lib/ruby/tumblr/tumblr-0.0.1/bin/tumblr

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://codeforpeople.com/
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top