Begining programmer questions

I was wondering, i thought in Ruby you didnt have to declare variables
but in this little exercise from the book i have the error i get is that
the variable info is undeclared. Basically the exercise is to keep
filling an array with user input until they choose exit and then the
array is sorted and printed out, is that how i would go about doing
that?

def input
  puts "Enter some info"
  info[] = gets.chomp
while info[] != "exit"
info[] = gets.chomp
end
info[].sort
puts "#{info}"
end

···

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

When you call 'info', the '' piece is actually trying to invoke a method
in the object.

since 'info' is not delcared as an object, Ruby has no clue what to do.

Now, if you did this:

info =

or

info = Array.new

You'd be better off.

As for stuffing 'info' with data, you can do this:

info << gets.comp

and

info.sort!

Does this help you at all?

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

I was wondering, i thought in Ruby you didnt have to declare variables
but in this little exercise from the book i have the error i get is that
the variable info is undeclared. Basically the exercise is to keep
filling an array with user input until they choose exit and then the
array is sorted and printed out, is that how i would go about doing
that?

def input
  puts "Enter some info"
  info = gets.chomp
while info != "exit"
info = gets.chomp
end
info.sort
puts "#{info}"
end

--
Posted via http://www.ruby-forum.com/\.

oh ok thats right it has to be an object in order to be useful. The book
i am reading didnt mention that in the entire chapter on arrays. thats
why i didnt get it.

Michael Gorsuch wrote:

···

When you call 'info', the '' piece is actually trying to invoke a
method
in the object.

since 'info' is not delcared as an object, Ruby has no clue what to do.

Now, if you did this:

info =

or

info = Array.new

You'd be better off.

As for stuffing 'info' with data, you can do this:

info << gets.comp

and

info.sort!

Does this help you at al

--
Posted via http://www.ruby-forum.com/\.

i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just saying
info you what what i mean? That would be even more simplified.

corey konrad wrote:

···

oh ok thats right it has to be an object in order to be useful. The book
i am reading didnt mention that in the entire chapter on arrays. thats
why i didnt get it.

Michael Gorsuch wrote:

When you call 'info', the '' piece is actually trying to invoke a
method
in the object.

since 'info' is not delcared as an object, Ruby has no clue what to do.

Now, if you did this:

info =

or

info = Array.new

You'd be better off.

As for stuffing 'info' with data, you can do this:

info << gets.comp

and

info.sort!

Does this help you at al

--
Posted via http://www.ruby-forum.com/\.

I suppose it could, but I think the easiest way to build an Array in ruby is
as follows:

info =

You can build a Hash via:

info = {}

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just saying
info you what what i mean? That would be even more simplified.

corey konrad wrote:
> oh ok thats right it has to be an object in order to be useful. The book
> i am reading didnt mention that in the entire chapter on arrays. thats
> why i didnt get it.
>
> Michael Gorsuch wrote:
>> When you call 'info', the '' piece is actually trying to invoke a
>> method
>> in the object.
>>
>> since 'info' is not delcared as an object, Ruby has no clue what to do.
>>
>> Now, if you did this:
>>
>> info =
>>
>> or
>>
>> info = Array.new
>>
>> You'd be better off.
>>
>> As for stuffing 'info' with data, you can do this:
>>
>> info << gets.comp
>>
>> and
>>
>> info.sort!
>>
>> Does this help you at al

--
Posted via http://www.ruby-forum.com/\.

A lot of people have the mistaken notion that Ruby is not "strongly typed"
(perhaps because they confuse dynamic type-resolution with weak typing), and
this is a good counterexample. You might suppose that Ruby could infer from
the syntax info= that it should create an object of type Array, but in
fact the method named = is defined on other classes (such as Hash), and
could of course be defined or meta-defined in your own classes. So Ruby
doesn't try to guess what class you meant.

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just saying
info you what what i mean? That would be even more simplified.

corey konrad wrote:
> oh ok thats right it has to be an object in order to be useful. The book
> i am reading didnt mention that in the entire chapter on arrays. thats
> why i didnt get it.
>
> Michael Gorsuch wrote:
>> When you call 'info', the '' piece is actually trying to invoke a
>> method
>> in the object.
>>
>> since 'info' is not delcared as an object, Ruby has no clue what to do.
>>
>> Now, if you did this:
>>
>> info =
>>
>> or
>>
>> info = Array.new
>>
>> You'd be better off.
>>
>> As for stuffing 'info' with data, you can do this:
>>
>> info << gets.comp
>>
>> and
>>
>> info.sort!
>>
>> Does this help you at al

--
Posted via http://www.ruby-forum.com/\.

The problem here is you are confusing declaring a variable with assigning a vlaue to a variable.

In ruby I can type
x = 0 # assigns 0 to x
without declaring the variable x.

In C I have to type
int x; /* declares variable named x */
x = 0; /* assigns 0 to it */

Acutally I could have typed

int x = 0;

but that's merely a more concise way of writing the declaration and the assigment.

Technically this is what happens in ruby as well, but declarations occur as a side effect of the first (spatial, not necessarily chronilogical) assignment.

You can see this with the following two pieces of code:

% cat example1.rb
puts x.inspect

% ruby example1.rb
example1.rb:1: undefined local variable or method `x' for main:Object (NameError)

You'll note that since x doesn't not appear in any preceding code as the left hand side of an assignment statement, the variable x is not declared. Likewise no method named x exists, so it raises a NameError

% cat example2.rb
if false
   x = 0
end

puts x.inspect

% ruby example2.rb
nil

Now this time no exception was raised, even though x never actually got set to zero. This is because ruby determines variable declarations when it parses the source, it makes not of any identifiers on the lhs of an assignment operator.

Now why can't this work for
info = blah
?

because = is a method call. Only identifier = expression is an actual assignment. Any number of objects can have different meanings for =.

info = blah

is

info.=(blah)

in actuality.

···

On May 14, 2006, at 6:17 PM, corey konrad wrote:

i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just saying
info you what what i mean? That would be even more simplified.

ok thanks for the help

Michael Gorsuch wrote:

···

I suppose it could, but I think the easiest way to build an Array in
ruby is
as follows:

info =

You can build a Hash via:

info = {}

--
Posted via http://www.ruby-forum.com/\.

you're talking over my head francis, i am a beginner. I have no idea
what strongly typed even means to be honest.

Francis Cianfrocca wrote:

···

A lot of people have the mistaken notion that Ruby is not "strongly
typed"
(perhaps because they confuse dynamic type-resolution with weak typing),
and
this is a good counterexample. You might suppose that Ruby could infer
from
the syntax info= that it should create an object of type Array, but in
fact the method named = is defined on other classes (such as Hash),
and
could of course be defined or meta-defined in your own classes. So Ruby
doesn't try to guess what class you meant.

--
Posted via http://www.ruby-forum.com/\.

Yeah isn't everything a first class object in Ruby.

Looked to me as though the issue was the scope since he originally declared it in
the def rather than in the scope of the caller where he wants to refer to it.

···

On May 14, 2006, at 5:20 PM, Michael Gorsuch wrote:

I suppose it could, but I think the easiest way to build an Array in ruby is
as follows:

info =

You can build a Hash via:

info = {}

On 5/14/06, corey konrad <0011@hush.com> wrote:

i dont know doing info = Array.new seems like an array declaration to
me. Couldn the constrution of an array object be implied in just saying
info you what what i mean? That would be even more simplified.

corey konrad wrote:
> oh ok thats right it has to be an object in order to be useful. The book
> i am reading didnt mention that in the entire chapter on arrays. thats
> why i didnt get it.
>
> Michael Gorsuch wrote:
>> When you call 'info', the '' piece is actually trying to invoke a
>> method
>> in the object.
>>
>> since 'info' is not delcared as an object, Ruby has no clue what to do.
>>
>> Now, if you did this:
>>
>> info =
>>
>> or
>>
>> info = Array.new
>>
>> You'd be better off.
>>
>> As for stuffing 'info' with data, you can do this:
>>
>> info << gets.comp
>>
>> and
>>
>> info.sort!
>>
>> Does this help you at al

--
Posted via http://www.ruby-forum.com/\.

well i tried what you said but i still get an error now, it says that i
have the wrong number of arguments. I know it isnt that important of
program but i just dont understand the error messages.

def input
  puts "Enter some info"
  info =
  info << gets.chomp
while info != "exit"
info << gets.chomp
end
info.sort
puts "#{info}"
end

corey konrad wrote:

···

ok thanks for the help

Michael Gorsuch wrote:

I suppose it could, but I think the easiest way to build an Array in
ruby is
as follows:

info =

You can build a Hash via:

info = {}

--
Posted via http://www.ruby-forum.com/\.

Corey -

A strongly type language would insist that you declare everything before
use.

Example, in C, you would have to do

"int my_variable" before you put anythign in it. And it better be an int
:wink:

In Ruby, you can just start working. Objects do have to be created, though
some can be figured out Ruby itself.

Even better, a variable can become something else. Example:

# 'c' is a string here
c = "some text"

# 'c' is now an array of strings
c =
c << "some more text"
c << "some other stuff"

You couldn't get away with this in a strictly typed language such as Java or
C - variables must always behave as they are commanded in the beginning.

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

you're talking over my head francis, i am a beginner. I have no idea
what strongly typed even means to be honest.

Francis Cianfrocca wrote:
> A lot of people have the mistaken notion that Ruby is not "strongly
> typed"
> (perhaps because they confuse dynamic type-resolution with weak typing),
> and
> this is a good counterexample. You might suppose that Ruby could infer
> from
> the syntax info= that it should create an object of type Array, but in
> fact the method named = is defined on other classes (such as Hash),
> and
> could of course be defined or meta-defined in your own classes. So Ruby
> doesn't try to guess what class you meant.

--
Posted via http://www.ruby-forum.com/\.

oh ok i needed to remove the in the while loop to, ok. Now when i
type exit the while loop ends but then the entire program ends instead
of sorting the aray and printing it...? I dont understand that

corey konrad wrote:

···

well i tried what you said but i still get an error now, it says that i
have the wrong number of arguments. I know it isnt that important of
program but i just dont understand the error messages.

def input
  puts "Enter some info"
  info =
  info << gets.chomp
while info != "exit"
info << gets.chomp
end
info.sort
puts "#{info}"
end

corey konrad wrote:

ok thanks for the help

Michael Gorsuch wrote:

I suppose it could, but I think the easiest way to build an Array in
ruby is
as follows:

info =

You can build a Hash via:

info = {}

--
Posted via http://www.ruby-forum.com/\.

while info != "exit"

this line is giving the error.

The '' piece expects an integer to placed in there to reference the
array. For example, info[0] or info[53].

I suggest something similar to this:

def input
  puts "Enter some info"
  info =
  while (data = gets.chomp) != 'exit'
    info << data
  end
  info.sort!
  puts info
end

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

well i tried what you said but i still get an error now, it says that i
have the wrong number of arguments. I know it isnt that important of
program but i just dont understand the error messages.

def input
  puts "Enter some info"
  info =
  info << gets.chomp
while info != "exit"
info << gets.chomp
end
info.sort
puts "#{info}"
end

corey konrad wrote:
> ok thanks for the help
>
> Michael Gorsuch wrote:
>> I suppose it could, but I think the easiest way to build an Array in
>> ruby is
>> as follows:
>>
>> info =
>>
>> You can build a Hash via:
>>
>> info = {}

--
Posted via http://www.ruby-forum.com/\.

Ruby is strongly typed, it's not statically typed. C has weaker types than ruby (Oh look, I just cast an int to a pointer). A statically typed language would insist you declare the types of everything before use. That is unless you have type inference.

e.g. at an ocaml prompt:
# let f x = x + 1;;
val f : int -> int = <fun>

You'll note I did not declare the type of x, nor the return type of the function, yet OCaml correctly deduced it. But that's tangential. Note also that typing doesn't necessarily determine whether or not you need to declare something before use. In both Javascript and Perl you can declare variables:

var x;

my $x;

Likewise, while I can't think of any languages that do this, I'm sure one could devise a statically typed language with no declarations.

···

On May 14, 2006, at 6:49 PM, Michael Gorsuch wrote:

Corey -

A strongly type language would insist that you declare everything before
use.

With some trepidation I'll commit the sin of threadjacking in order to
clarify some of this. I'm doing this because "weakly-typed" is an
unjustified criticism often made by Ruby's detractors, so I think it's
important for us to be able to answer it.

A strongly-typed language has well-defined, unambiguous rules about the
operations that may be performed on an object, and it refuses to perform
inferred type conversions. (Which is why Ruby won't let you add numbers to
strings as Perl does.) Ruby doesn't ever figure out for itself what type an
object is. If you create an object yourself, you need to type it (generally
with Klassname.new, or one of the shorthands like x=). Of course, if you
call a method that returns an object, that object will have a type
(determined by the writer of the method) and you can use it without
declaring it, but that doesn't mean Ruby figured out the type on its own.

Similarly, to say:
c = 100
c = "100"
is not dynamic re-typing, but rather a dynamic *rebinding* of a different
object to an already-used variable name. Perhaps the point you wanted to
make with this example is the distinction between static and dynamic
languages. In the former (C++, Java, etc), the type of every object in the
program must be determinable by the compiler, whereas in Python or Ruby
these determinations are not made until runtime. For people who come to Ruby
with strong experience in C/C++/Java, the lack of static typing is perhaps
the biggest shock. But it doesn't mean that Ruby is weakly-typed! Type
mismatches in Ruby always generate errors, but they occur at runtime rather
than compile-time.

I think the big controversy here is with people who reflexively reject the
idea that type errors should not be visible until runtime. It took me a long
time to get over this hump, but I now believe that dynamic type errors are
not a particularly nasty class of error and not to be feared.

Now that I'm a million miles away from Corey's original point, I'll go back
into my hole :-).

···

On 5/14/06, Michael Gorsuch <michael.gorsuch@gmail.com> wrote:

Corey -

A strongly type language would insist that you declare everything before
use.

Example, in C, you would have to do

"int my_variable" before you put anythign in it. And it better be an int
:wink:

In Ruby, you can just start working. Objects do have to be created,
though
some can be figured out Ruby itself.

Even better, a variable can become something else. Example:

# 'c' is a string here
c = "some text"

# 'c' is now an array of strings
c =
c << "some more text"
c << "some other stuff"

You couldn't get away with this in a strictly typed language such as Java
or
C - variables must always behave as they are commanded in the beginning.

On 5/14/06, corey konrad <0011@hush.com> wrote:
>
> you're talking over my head francis, i am a beginner. I have no idea
> what strongly typed even means to be honest.
>
> Francis Cianfrocca wrote:
> > A lot of people have the mistaken notion that Ruby is not "strongly
> > typed"
> > (perhaps because they confuse dynamic type-resolution with weak
typing),
> > and
> > this is a good counterexample. You might suppose that Ruby could infer
> > from
> > the syntax info= that it should create an object of type Array, but
in
> > fact the method named = is defined on other classes (such as Hash),
> > and
> > could of course be defined or meta-defined in your own classes. So
Ruby
> > doesn't try to guess what class you meant.
>
> --
> Posted via http://www.ruby-forum.com/\.
>

Just to be clear, will you please send your current source. I'd be glad to
look at it.

···

On 5/14/06, corey konrad <0011@hush.com> wrote:

oh ok i needed to remove the in the while loop to, ok. Now when i
type exit the while loop ends but then the entire program ends instead
of sorting the aray and printing it...? I dont understand that

corey konrad wrote:
> well i tried what you said but i still get an error now, it says that i
> have the wrong number of arguments. I know it isnt that important of
> program but i just dont understand the error messages.
>
> def input
> puts "Enter some info"
> info =
> info << gets.chomp
> while info != "exit"
> info << gets.chomp
> end
> info.sort
> puts "#{info}"
> end
>
> corey konrad wrote:
>> ok thanks for the help
>>
>> Michael Gorsuch wrote:
>>> I suppose it could, but I think the easiest way to build an Array in
>>> ruby is
>>> as follows:
>>>
>>> info =
>>>
>>> You can build a Hash via:
>>>
>>> info = {}

--
Posted via http://www.ruby-forum.com/\.

Hi --

With some trepidation I'll commit the sin of threadjacking in order to
clarify some of this. I'm doing this because "weakly-typed" is an
unjustified criticism often made by Ruby's detractors, so I think it's
important for us to be able to answer it.

A strongly-typed language has well-defined, unambiguous rules about the
operations that may be performed on an object, and it refuses to perform
inferred type conversions. (Which is why Ruby won't let you add numbers to
strings as Perl does.) Ruby doesn't ever figure out for itself what type an
object is. If you create an object yourself, you need to type it (generally
with Klassname.new, or one of the shorthands like x=). Of course, if you
call a method that returns an object, that object will have a type
(determined by the writer of the method) and you can use it without
declaring it, but that doesn't mean Ruby figured out the type on its own.

It sounds like you're talking about class, rather than type. A
string, for example (that is, an object that says "String" when you
ask it its class), can allow integers to be added to it, and is
therefore of class String but of *type* "string-like thing that lets
you add integers to it" (or whatever).

So the type of Ruby objects is almost a tautology: the type of object
o is "the type of objects that have the capabilities and interface of
object o." Class, meanwhile, though more tangible in a sense, is only
the starting point for the object's life-cycle.

David

···

On Mon, 15 May 2006, Francis Cianfrocca wrote:

--
David A. Black (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > Ruby for Rails

Michael Gorsuch wrote:

Just to be clear, will you please send your current source. I'd be glad
to
look at it.

this is it:

def input
  puts "Enter some info"
  info =
  info = gets.chomp
while info != "exit"
info = gets.chomp
end
info.sort
puts "#{info}"
end

···

--
Posted via http://www.ruby-forum.com/\.

Agreed. In fact, at its core, I would say that Ruby is "untyped," since everything is of the same type (Object). The only meaningful information about the qualifications of an object for a certain task is whether it responds to certain necessary methods, as you said. Based on this, I would say that Ruby simply attempts to blindly execute the code with _no_ type checking at all, but simply chokes if a method is missing. Is this a bad way to think of it?

- Jake McArthur

···

On May 15, 2006, at 12:33 AM, dblack@wobblini.net wrote:

It sounds like you're talking about class, rather than type. A
string, for example (that is, an object that says "String" when you
ask it its class), can allow integers to be added to it, and is
therefore of class String but of *type* "string-like thing that lets
you add integers to it" (or whatever).

So the type of Ruby objects is almost a tautology: the type of object
o is "the type of objects that have the capabilities and interface of
object o." Class, meanwhile, though more tangible in a sense, is only
the starting point for the object's life-cycle.