Function def at end of scripts

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-forum.com/.

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

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-forum.com/\.

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

stuff

BEGIN {
   def stuff
   end
}

a @ http://codeforpeople.com/

···

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-forum.com/\.

--
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/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm"
   }

   stuff

   BEGIN {
  def stuff
    puts "who would've guessed?"
  end
   }
# >> who would've guessed?
# >> The code in a block labeled with the keyword BEGIN is run automatically when a Ruby program is loaded : BEGIN END « Statement « Ruby

This is cool. Thanks!
einarmagnus

···

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-forum.com/\.

stuff

BEGIN {
def stuff
end
}

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

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

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-forum.com/\.

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-forum.com/.

Hi --

···

On Sat, 13 Dec 2008, Einar Magnús 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 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-forum.com/\.

stuff

BEGIN {
def stuff
end
}

a @ http://codeforpeople.com/
--
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/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm" }

stuff

BEGIN {
  def stuff
    puts "who would've guessed?"
  end
}
# >> who would've guessed?
# >> The code in a block labeled with the keyword BEGIN is run automatically when a Ruby program is loaded : BEGIN END « Statement « Ruby

This is cool. Thanks!

It can serve a purpose, but please have mercy on those of us who like
our Ruby in order :slight_smile: 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

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

···

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

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Hi --

On Sat, 13 Dec 2008, Einar Magnús 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 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-forum.com/\.

stuff
BEGIN {
def stuff
end
}
a @ http://codeforpeople.com/
--
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/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm" }

stuff

BEGIN {
  def stuff
    puts "who would've guessed?"
  end
}
# >> who would've guessed?
# >> The code in a block labeled with the keyword BEGIN is run automatically when a Ruby program is loaded : BEGIN END « Statement « Ruby

This is cool. Thanks!

It can serve a purpose, but please have mercy on those of us who like
our Ruby in order :slight_smile: 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

--
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 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' :wink: 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/

···

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 :slight_smile: 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.

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Hi --

It can serve a purpose, but please have mercy on those of us who like
our Ruby in order :slight_smile: 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

class Model
has_many :children do
  def extension
  end
end

the reality is that logical order and organization is far more important, consider reading a rails app 'in order' :wink: 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

···

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

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

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