Newbie ruby wonderment

Nothing important here, just wondering.

Started adding Ruby to my skills when I saw a Rails demo at at
convention.
Discovered real quick that I was going to have to learn a little Ruby
before the books on Rails made any sense in detail. So far so good.

I am curious why the convention in Ruby is to place the subroutines
-whoops, I mean methods - at the first of the program and the code at
the
bottom. Not that it makes any difference since callers and methods can
be
scattered thoughout, just like Perl and just about any other language.

Just seems strange to me after a lifetime of always adding the code
to the top and the newest routines to the bottom of the script and was
wondering who started the Ruby format?

Araminska in Tulsa

Ruby, like Perl, reads the code line by line, the same as you write it…

So you need to define methods before you call them, (this is interpreted
programming)

puts foo

def foo
‘bar’
end

#=> undefined local variable or method ‘foo’

Regards,
Lee

Sorry for slipping off-topic.

On Dec 6, 2007, at 5:20 PM, araminska wrote:

Araminska in Tulsa

Oklahoma? Always good to see a fellow Okie.

James Edward G. II

Ruby, like Perl, reads the code line by line, the same as you write it…

Sorry, but I have to disagree. (That is, if you are saying that Perl has
to have the subs first, also. Not sure you are.)

With perl

print_form()

sub print_form {
#… do stuff
}

works just fine. I have always put the subs at the bottom and the
calling
code at the top.

But you are right and I was wrong about Ruby. Methods DO have to be
defined before use. Not sure what I did before to think they don’t.
Perl
and Ruby obviously have different compiling structures.

No biggie. I just have to remember that till it becomes natural.

Araminska

On Dec 6, 10:16 pm, araminska [email protected] wrote:

#… do stuff
No biggie. I just have to remember that till it becomes natural.

Araminska

Seems that perl parses the entire file first, before it runs any code.
In most interpreted languages (ruby, python, lua, javascript, php,
&c), the code is executed as soon as an expression is parsed, so it is
an error to call a function before you define it.

Ps. Hope you enjoy using ruby. :slight_smile:

Regards,
Jordan

araminska wrote:

Sorry, but I have to disagree. (That is, if you are saying that Perl has
to have the subs first, also. Not sure you are.)

Yeah sorry, I went out for dinner after saying that and realized I was
wrong about Perl, but right about Ruby…

Perl is compiled at runtime… Although it is an interpreted language, it
does get compiled at runtime and therefore you can call subs before
they’re written…

Regards,
Lee

araminska wrote:

Perl and Ruby obviously have different compiling structures.

They don’t have ‘compiling structures’ - they’re interpreted languages.
:stuck_out_tongue:

“MonkeeSage” [email protected] wrote in message
news:[email protected]

defined before use. Not sure what I did before to think they don’t.
an error to call a function before you define it.
This is interesting 'cause Python is “compiled at runtime” (as Lee
Jarvis put it) and, yet, it shares the same limitation of needing
definitions before use as Ruby…

On Dec 7, 8:12 am, “Just Another Victim of the Ambient M.”
[email protected] wrote:

Sorry, but I have to disagree. (That is, if you are saying that Perl has

Araminska

Seems that perl parses the entire file first, before it runs any code.
In most interpreted languages (ruby, python, lua, javascript, php,
&c), the code is executed as soon as an expression is parsed, so it is
an error to call a function before you define it.

This is interesting 'cause Python is "compiled at runtime" (as Lee

Jarvis put it) and, yet, it shares the same limitation of needing
definitions before use as Ruby…

I’m not actually sure how python does it. I thought I had read a
thread on c.l.p a while ago that said it was compiled on the fly, but
I couldn’t find it, so I just asked again over there.

Regards,
Jordan

On Dec 6, 4:24 pm, Lee J. [email protected] wrote:

Ruby, like Perl, reads the code line by line, the same as you write it…

So you need to define methods before you call them, (this is interpreted
programming)

As a contrived example, see this code:

digit = rand 9
self.class.class_eval{
define_method( “foo#{digit}” ){
puts “The secret number is #{digit}”
}
}

foo0 rescue puts “It’s not 0”
foo1 rescue puts “It’s not 1”
foo2 rescue puts “It’s not 2”
foo3 rescue puts “It’s not 3”
foo4 rescue puts “It’s not 4”
foo5 rescue puts “It’s not 5”
foo6 rescue puts “It’s not 6”
foo7 rescue puts “It’s not 7”
foo8 rescue puts “It’s not 8”
foo9 rescue puts “It’s not 9”

#=> It’s not 0
#=> The secret number is 1
#=> It’s not 2
#=> It’s not 3
#=> It’s not 4
#=> It’s not 5
#=> It’s not 6
#=> It’s not 7
#=> It’s not 8
#=> It’s not 9

The method doesn’t exist until created by define_method. Something
similar to define_method occurs when you write “def bar … end”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs