Begining programmer questions


#1

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


#2

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?


#3

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


#4

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


#5

ok thanks for the help

Michael G. 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 = {}


#6

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 = {}


#7

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 G. 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 = {}


#8

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


#9

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.


#10

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 G. 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 = {}


#11

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

Francis C. 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.


#12

Michael G. 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


#13

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


#14

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.

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.


#15

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.


#16

Ah!

Take a look here:

“info = gets.chomp”
You’ve just changed ‘info’ into a String.

Instead, do this:

“info << gets.chomp”

This means ‘get some stuff, chomp it, and add it to the info Array’


#17

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

puts “Enter some info”


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

Oh boy.

info = [] # an array
info = gets.chomp # info is now a string, you’ve lost anything that
was in info
while info != “exit” # ok…
info = gets.chomp # you’re not adding to the back of the array,
you’re changing the meaning of info to another string.
end
info.sort # This will give back an array of sorted strings, broken on
newlines in info. There won’t be any newlines in info. Also you
probably meant sort!
puts “#{info}” # will always print exit


#18

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


#19

oh ok basicly the issue is that i just didnt understand what the gets
method was doing, it was creating another string? Wow i thought it just
GOT another string i didnt know it created one. Thats really confusing.
Puts and Gets have been really confusing methods for me so far.

also why does puts #{info} always print exit even if a user doesnt type
that in, that doesnt make sense to me at all.

Michael G. wrote:

Ah!

Take a look here:

“info = gets.chomp”
You’ve just changed ‘info’ into a String.

Instead, do this:

“info << gets.chomp”

This means ‘get some stuff, chomp it, and add it to the info Array’


#20

this stuff is discouraging, lol. Since i started learning to program i
feel like i am mentally retarded or something. Is it normal to have this
many problems with understanding simple things when you learn this
stuff, for some people it seems to just come naturally like they were
born into it.