Forum: Ruby Function def at end of scripts

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
No W. (Guest)
on 2008-12-12 19:13
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
Sebastian H. (Guest)
on 2008-12-12 19:38
(Received via mailing list)
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
The H. (Guest)
on 2008-12-12 20:14
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
Joel VanderWerf (Guest)
on 2008-12-12 21:29
(Received via mailing list)
No Where wrote:
> Eric
The END {...} construct can be helpful:

END {
   main
}

def main
end
Ara H. (Guest)
on 2008-12-13 02:10
(Received via mailing list)
On Dec 12, 2008, at 10:06 AM, No Where wrote:

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


stuff


BEGIN {
   def stuff
   end
}

a @ http://codeforpeople.com/
Einar Magnús Boson (Guest)
on 2008-12-13 02:42
(Received via mailing list)
On 13.12.2008, at 00:02 , ara.t.howard wrote:

>> if I put them at the beginning of the script before I need them. Is
> stuff
> 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/Thecodei...
"
   }

   stuff

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

This is cool. Thanks!
einarmagnus
Brian C. (Guest)
on 2008-12-13 11:00
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.
David A. Black (Guest)
on 2008-12-13 11:19
(Received via mailing list)
Hi --

On Sat, 13 Dec 2008, Einar Magnús Boson wrote:

>>> of the script for legibility. This didn't work. They are only available
>>
>> we can deny everything, except that we have the possibility of being
> }
> 
http://www.java2s.com/Code/Ruby/Statement/Thecodei...
>
> 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
Einar Magnús Boson (Guest)
on 2008-12-13 19:41
(Received via mailing list)
On 13.12.2008, at 09:11 , David A. Black wrote:

>>
>>>> all at
>>> a @ http://codeforpeople.com/
>>
>> This is cool. Thanks!
> --
> 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
use this. Still good to know of. Looking into it made me find some
other weird stuff I had never come across before though, like the flip-
flop operator that took me a while to figure out properly. That is
also something I don't see myself using though, it has a bit too
complicated semantics for my taste and feels like obfuscation just to
avoid an extra line.


einarmagnus
No W. (Guest)
on 2008-12-15 19:04
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
Ara H. (Guest)
on 2008-12-15 19:28
(Received via mailing list)
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://codeforpeople.com/lib/ruby/tumblr/tumblr-0....

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/
David A. Black (Guest)
on 2008-12-15 20:46
(Received via mailing list)
Hi --

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

>
> has_many :children do
> 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
This topic is locked and can not be replied to.