[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie ruby wonderment.

JustDoIt

12/6/2007 11:17:00 PM

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

Lee Jarvis

12/6/2007 11:25:00 PM

0

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
--
Posted via http://www.ruby-....

James Gray

12/6/2007 11:31:00 PM

0

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


JustDoIt

12/7/2007 4:16:00 AM

0

> 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

Peter Bunyan

12/7/2007 7:27:00 AM

0

araminska wrote:
> Perl and Ruby obviously have different compiling structures.

They don't have 'compiling structures' - they're interpreted languages.
:P

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

MonkeeSage

12/7/2007 7:59:00 AM

0

On Dec 6, 10:16 pm, araminska <n...@nowhere.com> wrote:
> > 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

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

Regards,
Jordan

Lee Jarvis

12/7/2007 8:06:00 AM

0

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
--
Posted via http://www.ruby-....

Randy R

12/7/2007 2:13:00 PM

0


"MonkeeSage" <MonkeeSage@gmail.com> wrote in message
news:22168293-d632-4314-aa6c-45d50baa1a75@i29g2000prf.googlegroups.com...
> On Dec 6, 10:16 pm, araminska <n...@nowhere.com> wrote:
>> > 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
>
> 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...



MonkeeSage

12/7/2007 2:25:00 PM

0

On Dec 7, 8:12 am, "Just Another Victim of the Ambient Morality"
<ihates...@hotmail.com> wrote:
> "MonkeeSage" <MonkeeS...@gmail.com> wrote in message
>
> news:22168293-d632-4314-aa6c-45d50baa1a75@i29g2000prf.googlegroups.com...
>
>
>
> > On Dec 6, 10:16 pm, araminska <n...@nowhere.com> wrote:
> >> > 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
>
> > 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

Phrogz

12/7/2007 2:34:00 PM

0

On Dec 6, 4:24 pm, Lee Jarvis <ljjar...@gmail.com> 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"