
-----------------------------------
dsantamassino
Thu Nov 04, 2004 10:56 pm

wtd, ruby questions
-----------------------------------
hi wtd,

here is my post on Ruby questions..



Save your program (yes, that's a program!) as calc.rb (the .rb is what we usually put at the end of programs written in Ruby). Now run your program by typing ruby calc.rb into your command line. It should have put a 3 on your screen. See, programming isn't so hard, now is it? 

ok i saved it as calc.rb but where and how do i or where do i type in ruby calc.rb ??

-----------------------------------
Hikaru79
Thu Nov 04, 2004 11:01 pm


-----------------------------------
I think he was assuming you were working in a UNIX environment, where 'ruby calc.rb' would look in the PATH for 'ruby' and then run it with the argument 'calc.rb'. 

Not sure how that would work in Windows. Does Windows even have a global path? Try going "Start->Run" and putting "ruby calc.rb" there... ionno. Check the documentation of wherever you installed the ruby interpreter/compiler.

-----------------------------------
dsantamassino
Thu Nov 04, 2004 11:03 pm


-----------------------------------
I think he was assuming you were working in a UNIX environment, where 'ruby calc.rb' would look in the PATH for 'ruby' and then run it with the argument 'calc.rb'. 

Not sure how that would work in Windows. Does Windows even have a global path? Try going "Start->Run" and putting "ruby calc.rb" there... ionno. Check the documentation of wherever you installed the ruby interpreter/compiler.

I did typed that in run just now and it flashed a black windows but nothing happened.

-----------------------------------
wtd
Thu Nov 04, 2004 11:04 pm


-----------------------------------
Which version of Windows are you using?

For Win 98, ME:

Go to Start -> Run, and type "command".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

For Win 2000, XP:

Go to Start -> Run, and type "cmd".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

-----------------------------------
wtd
Thu Nov 04, 2004 11:05 pm


-----------------------------------
I think he was assuming you were working in a UNIX environment, where 'ruby calc.rb' would look in the PATH for 'ruby' and then run it with the argument 'calc.rb'. 

Not sure how that would work in Windows. Does Windows even have a global path? Try going "Start->Run" and putting "ruby calc.rb" there... ionno. Check the documentation of wherever you installed the ruby interpreter/compiler.

I did typed that in run just now and it flashed a black windows but nothing happened.

It's doing something, but your program runs and then finishes and Windows thinks it's done so it closes the output window.  If you had a machine 100x slower, you might see something.  :)

-----------------------------------
dsantamassino
Thu Nov 04, 2004 11:08 pm


-----------------------------------
Which version of Windows are you using?

For Win 98, ME:

Go to Start -> Run, and type "command".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

For Win 2000, XP:

Go to Start -> Run, and type "cmd".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

I dont have a clue about commands. I saved it to my E drive so i went cd E:\ but i know thats wrong. Im running Win XP Pro SP2.

-----------------------------------
wtd
Thu Nov 04, 2004 11:10 pm


-----------------------------------
Which version of Windows are you using?

For Win 98, ME:

Go to Start -> Run, and type "command".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

For Win 2000, XP:

Go to Start -> Run, and type "cmd".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

I dont have a clue about commands. I saved it to my E drive so i went cd E:\ but i know thats wrong. Im running Win XP Pro SP2.

Ah.  To change directories...

C:\Whatever\Yada\Yada> E:

E:\> cd "the directory\where\I\saved\the file"

E:\the directory\where\I\saved\the file> ruby calc.rb

Should look something like that.  Learning how to use the command-line is key to learning how to program.

-----------------------------------
dsantamassino
Thu Nov 04, 2004 11:15 pm


-----------------------------------
Which version of Windows are you using?

For Win 98, ME:

Go to Start -> Run, and type "command".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

For Win 2000, XP:

Go to Start -> Run, and type "cmd".  This will give you a command prompt.  You'll need to "cd" (change directory) to the directory where you saved calc.rb.

I dont have a clue about commands. I saved it to my E drive so i went cd E:\ but i know thats wrong. Im running Win XP Pro SP2.

Ah.  To change directories...

C:\Whatever\Yada\Yada> E:

E:\> cd "the directory\where\I\saved\the file"

E:\the directory\where\I\saved\the file> ruby calc.rb

Is this right so far?? I dont think it is because i got an error.

E:\>cd ruby calc.rb\E

The system cannot find the path specified.

Should look something like that.  Learning how to use the command-line is key to learning how to program.

-----------------------------------
wtd
Thu Nov 04, 2004 11:20 pm


-----------------------------------
You don't want to cd to the file itself, just the directory it's in.

Consider if I start out in C:\ and I have calc.rb saved in E:\Programming\Ruby\

C:\> E:

E:\> cd "Programming\Ruby"

E:\Programming\Ruby>

Now I'm in the right directory.

E:\Programming\Ruby> ruby calc.rb

-----------------------------------
dsantamassino
Thu Nov 04, 2004 11:25 pm


-----------------------------------
You don't want to cd to the file itself, just the directory it's in.

Consider if I start out in C:\ and I have calc.rb saved in E:\Programming\Ruby\

C:\> E:

E:\> cd "Programming\Ruby"

E:\Programming\Ruby>

Now I'm in the right directory.

E:\Programming\Ruby> ruby calc.rb

ok it worked. Now let me read some more of the tutorial..

-----------------------------------
wtd
Thu Nov 04, 2004 11:29 pm


-----------------------------------
Happy hacking.  :)

-----------------------------------
dsantamassino
Fri Nov 05, 2004 6:18 pm


-----------------------------------
My next set of Ruby questions will be in this topic. At the rate im going i probably wont get nothing done..

-----------------------------------
wtd
Fri Nov 05, 2004 6:33 pm


-----------------------------------
My next set of Ruby questions will be in this topic. At the rate im going i probably wont get nothing done..

Just remember.. one step at a time.  Make sure you understand each idea before going on to the next.  The author of a tutorial will assume you do.

-----------------------------------
dsantamassino
Fri Nov 05, 2004 6:41 pm


-----------------------------------
My next set of Ruby questions will be in this topic. At the rate im going i probably wont get nothing done..

Just remember.. one step at a time.  Make sure you understand each idea before going on to the next.  The author of a tutorial will assume you do.

I got a great idea.. Im getting no where. Im more on break.. how about u and i go over the tutorial togther in this thread??

-----------------------------------
wtd
Fri Nov 05, 2004 6:44 pm


-----------------------------------
Fine with me.  Just give me some time to reply.  I'm writing an introduction/tutorial on Python's generators and iterators.

-----------------------------------
dsantamassino
Fri Nov 05, 2004 6:48 pm


-----------------------------------
Fine with me.  Just give me some time to reply.  I'm writing an introduction/tutorial on Python's generators and iterators.

Cool. Sure im here all night. For the pass week or so i had enough sugar in my system. right now i got always a 3 liter of sweten Ice Tea sitten next time. The rest of the days it was mountain dew so i will be sitting here doing programming. lol..

-----------------------------------
wtd
Fri Nov 05, 2004 6:55 pm


-----------------------------------
Ok... [url=http://pine.fm/LearnToProgram/?Chapter=01]Numbers.

Any questions here?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 6:58 pm


-----------------------------------
Ok... 

Yes hardly not a clue. before i was just reading it i didnt have a clue what to do. I personally need u to walk me through it if possible?? but i dont know..

-----------------------------------
wtd
Fri Nov 05, 2004 6:59 pm


-----------------------------------
Have you got irb open?  If you do, you should see a prompt like: 

irb(main):001:0>

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:01 pm


-----------------------------------
Have you got irb open?  If you do, you should see a prompt like: 

irb(main):001:0>

ok its open.

-----------------------------------
wtd
Fri Nov 05, 2004 7:05 pm


-----------------------------------
irb(main):001:0> puts 1 + 2
3
=> nil
irb(main):002:0>

Do you see what's going on here?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:07 pm


-----------------------------------
irb(main):001:0> puts 1 + 2
3
=> nil
irb(main):002:0>

Do you see what's going on here?

well first. I have it open but didnt write anything in there. I dont know what to write at that line so how can i know whats going on?? Its not my fault i was born to learn slow..

-----------------------------------
wtd
Fri Nov 05, 2004 7:11 pm


-----------------------------------
irb(main):001:0> puts 1 + 2
3
=> nil
irb(main):002:0>

Do you see what's going on here?

well first. I have it open but didnt write anything in there. I dont know what to write at that line so how can i know whats going on?? Its not my fault i was born to learn slow..

I typed "puts 1 + 2" at the prompt and hit enter in the above example.

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:13 pm


-----------------------------------
irb(main):001:0> puts 1 + 2
3
=> nil
irb(main):002:0>

Do you see what's going on here?

well first. I have it open but didnt write anything in there. I dont know what to write at that line so how can i know whats going on?? Its not my fault i was born to learn slow..

I typed "puts 1 + 2" at the prompt and hit enter in the above example.

ok i got that

-----------------------------------
wtd
Fri Nov 05, 2004 7:18 pm


-----------------------------------
The "1 + 2" part takes two numbers and adds them together.  Not surprisingly, the result is 3.

The "puts" takes that result and prints it out.

"=> nil" shows the result of "puts".  In this case it's basically "nothing".

And then you see the prompt again, ready for the next thing.

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:19 pm


-----------------------------------
The "1 + 2" part takes two numbers and adds them together.  Not surprisingly, the result is 3.

The "puts" takes that result and prints it out.

"=> nil" shows the result of "puts".  In this case it's basically "nothing".

And then you see the prompt again, ready for the next thing.

Ok yes im ready.. will i learn Ruby tonight or will it take sometime??

-----------------------------------
wtd
Fri Nov 05, 2004 7:28 pm


-----------------------------------
The "1 + 2" part takes two numbers and adds them together.  Not surprisingly, the result is 3.

The "puts" takes that result and prints it out.

"=> nil" shows the result of "puts".  In this case it's basically "nothing".

And then you see the prompt again, ready for the next thing.

Ok yes im ready.. will i learn Ruby tonight or will it take sometime??

I can't really say.  It's doubtful you'll learn all of the basics in one night, especially approaching it without prior experience (I tackled Ruby already knowing several languages, so my experience is going to be different from yours).

I think you could get all of the basics in a week or two.  Of course, there's a lot more there beyond the basics, but that's true of any programming language.

irb(main):002:0> puts 5 * (12-8) + -15
5
=> nil
irb(main):003:0>

Do you understand what's going on here?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:33 pm


-----------------------------------
The "1 + 2" part takes two numbers and adds them together.  Not surprisingly, the result is 3.

The "puts" takes that result and prints it out.

"=> nil" shows the result of "puts".  In this case it's basically "nothing".

And then you see the prompt again, ready for the next thing.

Ok yes im ready.. will i learn Ruby tonight or will it take sometime??

I can't really say.  It's doubtful you'll learn all of the basics in one night, especially approaching it without prior experience (I tackled Ruby already knowing several languages, so my experience is going to be different from yours).

I think you could get all of the basics in a week or two.  Of course, there's a lot more there beyond the basics, but that's true of any programming language.

irb(main):002:0> puts 5 * (12-8) + -15
5
=> nil
irb(main):003:0>

Do you understand what's going on here?

Yes sir i understand but its been a while for math especially algebra. Let me get my orders straight. Im 22 btw..

12-8 first would bring it down to 5*4 + -15
20 + -15
5

holy shit i got it. wow baby. lol

-----------------------------------
wtd
Fri Nov 05, 2004 7:38 pm


-----------------------------------
I'm only 24, young'en.  ;)

And congrats, but limit the profanity.  We don't want school filters restricting access to this site.  :)

-----------------------------------
dsantamassino
Fri Nov 05, 2004 7:39 pm


-----------------------------------
I'm only 24, young'en.  ;)

And congrats, but limit the profanity.  We don't want school filters restricting access to this site.  :)

Ok sorry.

-----------------------------------
wtd
Fri Nov 05, 2004 7:53 pm


-----------------------------------
Now, write a line of code which displays the number of seconds in a year.  :)

-----------------------------------
dsantamassino
Fri Nov 05, 2004 8:39 pm


-----------------------------------
Now, write a line of code which displays the number of seconds in a year.  :)

No offense but are you on drugs?

my god. I dont know that much math or how to do it. go smoke another one.

-----------------------------------
JHanson90
Fri Nov 05, 2004 8:51 pm


-----------------------------------
No offense but are you on drugs?

my god. I dont know that much math or how to do it. go smoke another one.
Although I myself may only be 14, you don't strike me as 22... respect those who help you out. :)

-----------------------------------
wtd
Fri Nov 05, 2004 8:51 pm


-----------------------------------
How many seconds are in a minute?

How many minutes are in an hour?

How many hours are in a day?

How many days are in a year?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 9:05 pm


-----------------------------------
How many seconds are in a minute?

How many minutes are in an hour?

How many hours are in a day?

How many days are in a year?

there are 60 minutes in an hour
there are 24 hours in a day
there are 365 days in a year. So i would be like this??

60 * 24 * 365
525600

-----------------------------------
dsantamassino
Fri Nov 05, 2004 9:05 pm


-----------------------------------
How many seconds are in a minute?

How many minutes are in an hour?

How many hours are in a day?

How many days are in a year?

there are 60 minutes in an hour
there are 24 hours in a day
there are 365 days in a year. So i would be like this??

60 * 24 * 365
525600

-----------------------------------
wtd
Fri Nov 05, 2004 9:07 pm


-----------------------------------
How many seconds are in a minute?

How many minutes are in an hour?

How many hours are in a day?

How many days are in a year?

there are 60 minutes in an hour
there are 24 hours in a day
there are 365 days in a year. So i would be like this??

60 * 24 * 365
525600

That's the number of minutes in a year.... so just multiply by the number of seconds in a minute and you have the number of seconds in a year.

Now, convert it to Ruby code.  (Hint: You're 99% of the way there already)

-----------------------------------
dsantamassino
Fri Nov 05, 2004 9:14 pm


-----------------------------------
How many seconds are in a minute?

How many minutes are in an hour?

How many hours are in a day?

How many days are in a year?

there are 60 minutes in an hour
there are 24 hours in a day
there are 365 days in a year. So i would be like this??

60 * 24 * 365
525600

That's the number of minutes in a year.... so just multiply by the number of seconds in a minute and you have the number of seconds in a year.

Now, convert it to Ruby code.  (Hint: You're 99% of the way there already)

so it would be?? 525600*60=31536000

Now how do i convert it to Ruby code??

-----------------------------------
wtd
Fri Nov 05, 2004 9:23 pm


-----------------------------------
Hint: note what happens when you run the following in irb.

puts 5 * 4 * 2

-----------------------------------
dsantamassino
Fri Nov 05, 2004 9:46 pm


-----------------------------------
Hint: note what happens when you run the following in irb.

puts 5 * 4 * 2

40
=> nil

-----------------------------------
wtd
Fri Nov 05, 2004 9:51 pm


-----------------------------------
Exactly.  See the pattern?  Several numbers multiplied and the result displayed.  Use the same pattern to output the result of multiplying seconds * minutes * hours * days.

-----------------------------------
dsantamassino
Fri Nov 05, 2004 9:53 pm


-----------------------------------
oh ok..

yeah i understand and i see the pattern..

-----------------------------------
wtd
Fri Nov 05, 2004 9:59 pm


-----------------------------------
Good.

Moving on...

Do you know why 4 is different from 4.0?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 10:01 pm


-----------------------------------
Good.

Moving on...

Do you know why 4 is different from 4.0?

Not really.

-----------------------------------
wtd
Fri Nov 05, 2004 10:09 pm


-----------------------------------
Good.

Moving on...

Do you know why 4 is different from 4.0?

Not really.

4

Is an integer.  It's a whole number.

4.0

Is a floating point number.  It has a decimal part in addition to a whole number.

Integers are generally more useful.  We mostly consider 1, 2, 3, etc. "things", rather than halves of "things".

For the most part, Ruby handles the differences between integers and floats pretty transparently, but the difference does show up in math.

When you do math between two integers, the result is always another integer.  But introduce a single float into the mix and the result is a float.

3 / 2

Is 1.

3 / 2.0

Is 1.5.

Does this make sense?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 11:11 pm


-----------------------------------
Good.

Moving on...

Do you know why 4 is different from 4.0?

Not really.

4

Is an integer.  It's a whole number.

4.0

Is a floating point number.  It has a decimal part in addition to a whole number.

Integers are generally more useful.  We mostly consider 1, 2, 3, etc. "things", rather than halves of "things".

For the most part, Ruby handles the differences between integers and floats pretty transparently, but the difference does show up in math.

When you do math between two integers, the result is always another integer.  But introduce a single float into the mix and the result is a float.

3 / 2

Is 1.

3 / 2.0

Is 1.5.

Does this make sense?

Yes that makes sense.

-----------------------------------
wtd
Fri Nov 05, 2004 11:15 pm


-----------------------------------
Do you understand what a string is?

-----------------------------------
dsantamassino
Fri Nov 05, 2004 11:17 pm


-----------------------------------
Do you understand what a string is?

No. I heard of a string but dont know what it is.

-----------------------------------
wtd
Fri Nov 05, 2004 11:21 pm


-----------------------------------
A string is a sequence of characters.

"Hello world!"

Is a string.  Strings are enclosed in either double quotes, as above, or in single quotes.  There are slight differences between them, but generally you can use the interchangeably in simple examples.

Make sense?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:53 am


-----------------------------------
A string is a sequence of characters.

"Hello world!"

Is a string.  Strings are enclosed in either double quotes, as above, or in single quotes.  There are slight differences between them, but generally you can use the interchangeably in simple examples.

Make sense?

Yeah that kind of make sense. OK lets move on..

-----------------------------------
wtd
Sat Nov 06, 2004 3:09 pm


-----------------------------------
On to string math.

You can use + and * on strings as well as numbers.

"foo" + "bar"

is

"foobar"

and, since...

4 * 3

is just

4 + 4 + 4

then

"foo" * 3

is just

"foo" + "foo" + "foo"

Which is

"foofoofoo"

Any questions?

You should be trying these in irb as we go.

-----------------------------------
dsantamassino
Sat Nov 06, 2004 6:53 pm


-----------------------------------
On to string math.

You can use + and * on strings as well as numbers.

"foo" + "bar"

is

"foobar"

and, since...

4 * 3

is just

4 + 4 + 4

then

"foo" * 3

is just

"foo" + "foo" + "foo"

Which is

"foofoofoo"

Any questions?

You should be trying these in irb as we go.

Yes i do have a question. I put in 4 * 3 and it gave me 12. So i put in "4" * 3 and gave me 444. I put in "4" * "3". Cannot converting string to integer. Why didnt it gave me 4 + 4 + 4??

-----------------------------------
wtd
Sat Nov 06, 2004 7:05 pm


-----------------------------------
Ah, you've struck on an interesting point.

"3"

Is a string that contains the character 3.

3

Is an actual number.

In something like:

"4" * 3

The * is expecting the next "thing" to be a number.  Similarly, "4" + 3 won't work, because the + is expecting another string.

Strings can be converted to numbers, and vice-versa... but first, does the above make sense?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 7:23 pm


-----------------------------------
Ah, you've struck on an interesting point.

"3"

Is a string that contains the character 3.

3

Is an actual number.

In something like:

"4" * 3

The * is expecting the next "thing" to be a number.  Similarly, "4" + 3 won't work, because the + is expecting another string.

Strings can be converted to numbers, and vice-versa... but first, does the above make sense?

As in this quote not a clue. Now u really got me. I understood the rest of the replies but not this one.

-----------------------------------
wtd
Sat Nov 06, 2004 7:34 pm


-----------------------------------
In programming, we commonly deal with different types of data.  Remember that word.  You'll hear it so many times it'll eventually drive you mad.  ;)

Follow along, young padawan.  

Err... excuse me.  I'm quite insane, you see, and you have to make allowances for my flights of fancy.

Anyway, we deal with different types of data.  So far we've seen three of them.  

Integers - whole numbers.  1, 2, 3 etc.  But not 1.23, 2.6, 4.2, etc.
Floats - numbers that can have a decimal component.  The catch is that they aren't always 100% accurate once you start dealing with really really big numbers.
Strings - sequences of letters, numbers, etc.

The pattern when we "concatenate" (add) strings is:

string + string

And the pattern when you multiply strings is:

string * integer or float

If you use a float in that one, it just uses the whole number part.

So...

"hello " * 3.5

Is

"hello hello hello "

-----------------------------------
dsantamassino
Sat Nov 06, 2004 7:39 pm


-----------------------------------
In programming, we commonly deal with different types of data.  Remember that word.  You'll hear it so many times it'll eventually drive you mad.  ;)

Follow along, young padawan.  

Err... excuse me.  I'm quite insane, you see, and you have to make allowances for my flights of fancy.

Anyway, we deal with different types of data.  So far we've seen three of them.  

Integers - whole numbers.  1, 2, 3 etc.  But not 1.23, 2.6, 4.2, etc.
Floats - numbers that can have a decimal component.  The catch is that they aren't always 100% accurate once you start dealing with really really big numbers.
Strings - sequences of letters, numbers, etc.

The pattern when we "concatenate" (add) strings is:

string + string

And the pattern when you multiply strings is:

string * integer or float

If you use a float in that one, it just uses the whole number part.

So...

"hello " * 3.5

Is

"hello hello hello "

oh ok i see.

-----------------------------------
wtd
Sat Nov 06, 2004 7:54 pm


-----------------------------------
So I mentioned that you can go back and forth between these different types.  How though, remains the question.

A quick look at a slightly more advanced aspect of Ruby.  Everything is an object - one or more pieces of data with various operations, all bundled together in nice neat package.

Don't worry about that yet, though.

All you need to know are three of those operations:


to_s - turn this bit of data into a string
to_i - turn this bit of data into an integer
to_f - turn this bit of data into a float

So, in our previous examples, we needed an integer (or float, but integer is preferred) to multiply a string by, and another string that looked like an integer wouldn't do.  Well, now...

"4" * "3".to_i

Works just fine.

Or even...

4.to_s * "3".to_i

Note:  Unless you're asking me about a very specific point in one of my previous posts, or a rather old post, there's no need to quote my entire post.  :)

-----------------------------------
dsantamassino
Sat Nov 06, 2004 9:08 pm


-----------------------------------
ok i understand that.



I dont know how to quote part of a post and leave part out. I only know how to quote the full post and sometimes i screw up with that too..

-----------------------------------
wtd
Sat Nov 06, 2004 9:15 pm


-----------------------------------
Variables.  They're a big scary programming topic.  They bring strong men to tears and cause virtuous women to faint.

Not!

Variables are just a way of giving names to the same things we've been dealing with anyway.

Instead of just haveing 

"4"

In your previous example, let's create a variable named "four" to stand for that value.

four = "4"

Then, instead of:

"4" * 3

We can write:

four * 3

-----------------------------------
dsantamassino
Sat Nov 06, 2004 9:17 pm


-----------------------------------
Ok i understand that..

-----------------------------------
wtd
Sat Nov 06, 2004 9:20 pm


-----------------------------------
Just to make sure, you've been trying these on your own and seeing the right answers, right?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 9:23 pm


-----------------------------------
Just to make sure, you've been trying these on your own and seeing the right answers, right?

yeah. Want me take a screen shot of my commands and u hot link it to the board from a server??

-----------------------------------
wtd
Sat Nov 06, 2004 9:29 pm


-----------------------------------
No, that's ok.  I'll take your word for it. 

Now... back to our regularly scheduled trainwreck...

Variables names are pretty much anything you want them to be, so long as they start with a lower case letter.  "four" could be "four", or "integerificus_the_grand" as far as Ruby cares.  Of course, you should use variable names that mean something to you, and you probably want something that's easier to type than "integerificus_the_grand".

-----------------------------------
dsantamassino
Sat Nov 06, 2004 9:31 pm


-----------------------------------
right. alright i understand. Its pretty simple.

-----------------------------------
wtd
Sat Nov 06, 2004 10:11 pm


-----------------------------------
We've seen assignment to a variable.  It looks like:

son_of_integerificus = 42

Of course, we can assign more complex things to a variable.

stringali_the_small = "foo" * son_of_integerificus

And there are shortcuts for assignments where we modify the variable.

son_of_integerificus = son_of_integerificus * 2

Can become:

son_of_integerificus *= 2

Similarly, we can do the same for +, -, /, and %.

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:20 pm


-----------------------------------
ok i wrote that in my irb..

-----------------------------------
wtd
Sat Nov 06, 2004 10:22 pm


-----------------------------------
Did you see what you expected to?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:27 pm


-----------------------------------
Did you see what you expected to?

son_of_integerificus = son_of_integerificus * 2
=> 84

Is that right?? that was from irb.

-----------------------------------
wtd
Sat Nov 06, 2004 10:37 pm


-----------------------------------
Sweet.

More about "puts".  We can look at this and think it's just the english word "puts", but what it really is, is put string.

So:

puts 42

Is really:

puts 42.to_s

But you don't need to type out the ".to_s".

So, if "puts" is put string, can you guess what "gets" means, and what it does?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:40 pm


-----------------------------------
Sweet.

More about "puts".  We can look at this and think it's just the english word "puts", but what it really is, is put string.

So:

puts 42

Is really:

puts 42.to_s

But you don't need to type out the ".to_s".

So, if "puts" is put string, can you guess what "gets" means, and what it does?

I would say gets means get string and i wouldnt know what it means.. maybe to get a string i dont know. am i right??

-----------------------------------
wtd
Sat Nov 06, 2004 10:42 pm


-----------------------------------
You're on the right (and very obvious :wink:) track.

It's get string, and it's purpose is to get a string of input from the user.

Try:

gets

And tell me what you see.  :)

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:44 pm


-----------------------------------
"\n"

-----------------------------------
wtd
Sat Nov 06, 2004 10:48 pm


-----------------------------------
This is because you hit enter, and that's what that character looks like to Ruby.  Try it again and try, for the heck of it, typing your name.

-----------------------------------
dsantamassino
Sat Nov 06, 2004 10:49 pm


-----------------------------------
I typed gets
dsantamassino
enter
"dsantamassino\n"

-----------------------------------
wtd
Sat Nov 06, 2004 10:52 pm


-----------------------------------
Nifty... so now you get input from the user.

Time to learn how to create files and run those.  

Do you know how to change directories in DOS?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 11:01 pm


-----------------------------------
Nifty... so now you get input from the user.

Time to learn how to create files and run those.  

Do you know how to change directories in DOS?

kind of but not to well. I know i would type C: or something to get it on my drive but not sure.

-----------------------------------
wtd
Sat Nov 06, 2004 11:06 pm


-----------------------------------
Yep.  

First off, save the following simple program in a file named "greet.rb".

puts "Your name is?"
name = gets
puts "Hello, " + name + "!"

Remember where you saved it.

-----------------------------------
dsantamassino
Sat Nov 06, 2004 11:10 pm


-----------------------------------
ok i did save it.

-----------------------------------
wtd
Sat Nov 06, 2004 11:12 pm


-----------------------------------
Open up a command prompt.  Start -> Run, type "cmd" and hit enter.

Now, get to the drive where you saved the file.

-----------------------------------
dsantamassino
Sat Nov 06, 2004 11:28 pm


-----------------------------------
I went to my C drive and then i typed in greet that didnt work so i typed in greet.rb and that didnt work..

-----------------------------------
wtd
Sat Nov 06, 2004 11:33 pm


-----------------------------------
Did you save the file at the root of your C drive?

-----------------------------------
dsantamassino
Sat Nov 06, 2004 11:34 pm


-----------------------------------
Yes

Did you save the file at the root of your C drive?

-----------------------------------
wtd
Sat Nov 06, 2004 11:40 pm


-----------------------------------
Type "dir /B" and hit enter.  See if greet.rb is in that list.

-----------------------------------
dsantamassino
Sun Nov 07, 2004 7:06 am


-----------------------------------
No its not there.

-----------------------------------
wtd
Sun Nov 07, 2004 5:06 pm


-----------------------------------
I thought not.

You'll need to "cd" to the directory (or "folder") greet.rb is in.  The basic format is:

C:\> cd "the folder\the file\is in"

-----------------------------------
dsantamassino
Sun Nov 07, 2004 6:09 pm


-----------------------------------
Not a clue what to do here. this is what i typed and didnt work.

It was already pointed to the C: drive so on that line i typed.

cd C\greet.rb

-----------------------------------
Hikaru79
Sun Nov 07, 2004 10:33 pm


-----------------------------------
Nono, what he means is that first you have to cd into the directory with the .rb file in it. So if the .rb file is in "C:\Documents\Programming\Ruby\" then type:

cd c:
cd Documents
cd Programming
cd Ruby
ruby greet.rb


Or, if you're feeling adventurous,

cd c:\Documents\Programming\Ruby\
ruby greet.rb

If it's already IN root C;\ and that's where you are by defult, you don't need to cd anywhere. Just: ruby greet.rb

-----------------------------------
dsantamassino
Sun Nov 07, 2004 10:48 pm


-----------------------------------
Nono, what he means is that first you have to cd into the directory with the .rb file in it. So if the .rb file is in "C:\Documents\Programming\Ruby\" then type:

cd c:
cd Documents
cd Programming
cd Ruby
ruby greet.rb


Or, if you're feeling adventurous,

cd c:\Documents\Programming\Ruby\
ruby greet.rb

If it's already IN root C;\ and that's where you are by defult, you don't need to cd anywhere. Just: ruby greet.rb

It already on my root C so i typed ruby greet.rb and this is the message im getting.

ruby: No such file or directory -- greet.rb (LoadError)

-----------------------------------
wtd
Mon Nov 08, 2004 12:29 am


-----------------------------------
Ok... the file is obviously not in C:\, so trying to run it from there will not work.

Where exactly did you save the file?  Use the search feature if you have to.

-----------------------------------
dsantamassino
Mon Nov 08, 2004 6:29 pm


-----------------------------------
Ok... the file is obviously not in C:\, so trying to run it from there will not work.

Where exactly did you save the file?  Use the search feature if you have to.

well i did a search and it says C:\

-----------------------------------
wtd
Mon Nov 08, 2004 7:12 pm


-----------------------------------
Is it really "greet.rb", or did it get saved as "greet.rb.txt"?

-----------------------------------
dsantamassino
Mon Nov 08, 2004 7:43 pm


-----------------------------------
Is it really "greet.rb", or did it get saved as "greet.rb.txt"?

No i just looked. Its really on my root C: and its named greet.rb

so i tried greet.rb and ruby greet.rb both in the command and still nothing. Its doesnt recognize for some reason

-----------------------------------
wtd
Mon Nov 08, 2004 8:28 pm


-----------------------------------
Ok... not gonna give up on this.

Try "type greet.rb" at the command-line.  The Windows command prompt, not the IRB prompt.

-----------------------------------
dsantamassino
Mon Nov 08, 2004 8:32 pm


-----------------------------------
Ok... not gonna give up on this.

Try "type greet.rb" at the command-line.  The Windows command prompt, not the IRB prompt.

I tried at the C: command line both as followed and nothing

type greet.rb
"type greet.rb"

exactly both ways with quotes and without.....

-----------------------------------
wtd
Mon Nov 08, 2004 8:46 pm


-----------------------------------
Ok, let's try a different approach...  instead of using Notepad or Textpad... for now we'll just used the built-in edit program.

At the command-line, enter:

C:\> edit greet.rb

You'll see a blue editor screen.  Here you can type in the program again, unless it's already there.  ;)

puts "Your name is?"
name = gets
puts "Hello, " + name + "!"

You can use Alt-F-S to save, and Alt-F-X to exit.

Then to run it:

C:\> ruby greet.rb

-----------------------------------
dsantamassino
Mon Nov 08, 2004 9:34 pm


-----------------------------------
ok.. here is a different error message down below..

greet.rb:3: syntax error
puts "Hello, " + Derek "!"

-----------------------------------
wtd
Mon Nov 08, 2004 9:43 pm


-----------------------------------
ok.. here is a different error message down below..

greet.rb:3: syntax error
puts "Hello, " + Derek "!"

Cool.

You're missing a + between Derek and "!".

The name of the variable should also be "name".  :)

-----------------------------------
dsantamassino
Mon Nov 08, 2004 9:48 pm


-----------------------------------
ok.. here is a different error message down below..

greet.rb:3: syntax error
puts "Hello, " + Derek "!"

Cool.

You're missing a + between Derek and "!".

The name of the variable should also be "name".  :)

Cool it worked.
Derek?

-----------------------------------
wtd
Mon Nov 08, 2004 10:41 pm


-----------------------------------
Spiffy.

Now, do you understand why the program went something like:

C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!

C:\>

When it should have looked like:

C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!

C:\>

-----------------------------------
dsantamassino
Mon Nov 08, 2004 10:44 pm


-----------------------------------
Spiffy.

Now, do you understand why the program went something like:

C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!

C:\>

When it should have looked like:

C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!

C:\>

No i dont know. May u explain it to me??

-----------------------------------
wtd
Mon Nov 08, 2004 10:48 pm


-----------------------------------
Sure.

When you call "gets" to get input from the user, you get all of their input, including the return key.

Then, then you use "puts" to output, you get that return again.

The key is to use another method named "chomp" to remove the return from the end of that string.

puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!"

-----------------------------------
dsantamassino
Mon Nov 08, 2004 10:50 pm


-----------------------------------
Sure.

When you call "gets" to get input from the user, you get all of their input, including the return key.

Then, then you use "puts" to output, you get that return again.

The key is to use another method named "chomp" to remove the return from the end of that string.

puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!"

Oh ok i think i got it

-----------------------------------
wtd
Mon Nov 08, 2004 11:09 pm


-----------------------------------
An exercise, shamelessly stolen from Chris Pine:

Write a program which asks for a person's first name, then middle, then last. Finally, it should greet the person using their full name.

-----------------------------------
dsantamassino
Tue Nov 09, 2004 5:48 pm


-----------------------------------
I tired to do it but its pointed an upward error to the middle name

-----------------------------------
wtd
Tue Nov 09, 2004 6:20 pm


-----------------------------------
Post the code you write.  Please surround it with code tags, like so:

[code]This is formatted as code.[/code]

-----------------------------------
dsantamassino
Tue Nov 09, 2004 6:38 pm


-----------------------------------
Post the code you write.  Please surround it with code tags, like so:

[code]This is formatted as code.[/code]

I dont know how nor what u mean.. I barely can qoute so i dont know how to use the code tags

-----------------------------------
wtd
Tue Nov 09, 2004 6:43 pm


-----------------------------------
Copy and paste your code into a post.  At the beginning of your code, type:

[code ]

And at the end of it, type:

[/code ]

Just don't use spaces between the e and ].

-----------------------------------
dsantamassino
Tue Nov 09, 2004 7:08 pm


-----------------------------------
when i went to edit greet.rb.. I cant figure out how to litteral copy or highlight the text to copy it then come to this board by clicking on the right my mouse then paste. Sorry but i dont know how to do it..

-----------------------------------
wtd
Tue Nov 09, 2004 7:15 pm


-----------------------------------
Select the text normally with your mouse.  DOS-style applications are a bit weird about selecting text.  Start at the top-left corner and drag down to the bottom right corner.  

While the text is selected, right-click.  Congratulations.  You just copied test the DOS way.

Right-click again in a DOS-style app to paste.

In a normal program, just paste normally.

-----------------------------------
dsantamassino
Tue Nov 09, 2004 9:59 pm


-----------------------------------
Select the text normally with your mouse.  DOS-style applications are a bit weird about selecting text.  Start at the top-left corner and drag down to the bottom right corner.  

While the text is selected, right-click.  Congratulations.  You just copied test the DOS way.

Right-click again in a DOS-style app to paste.

In a normal program, just paste normally.

I have not a single clue what your talking about. From what i have saved on my C: drive it says something totally different from the edit greet.rb file. I dont have a clue how to copy it. It will NOT let me copy it from the command window..

-----------------------------------
wtd
Tue Nov 09, 2004 10:10 pm


-----------------------------------
It will NOT let me copy it from the command window..

Yes.  It will.

Can you find no way to show me the program you wrote, so I can help you further?

-----------------------------------
dsantamassino
Tue Nov 09, 2004 10:15 pm


-----------------------------------
well i found out how to copy it from edit greet.rb from start-->> Run-->>cmd but now can figure out how to paste it to this board. when i right clicked on this board its not pasting my output..

-----------------------------------
wtd
Tue Nov 09, 2004 10:18 pm


-----------------------------------
well i found out how to copy it from edit greet.rb from start-->> Run-->>cmd but now can figure out how to paste it to this board. when i right clicked on this board its not pasting my output..

Just right-click and select "Paste".

-----------------------------------
dsantamassino
Tue Nov 09, 2004 10:21 pm


-----------------------------------
well i found out how to copy it from edit greet.rb from start-->> Run-->>cmd but now can figure out how to paste it to this board. when i right clicked on this board its not pasting my output..

Just right-click and select "Paste".

Yeah thats what i did but its not pasting my output

-----------------------------------
wtd
Tue Nov 09, 2004 10:30 pm


-----------------------------------
Ok, stick with me, I think I can help with one last idea.

C:\> notepad greet.rb

That should open the file in a new Notepad window, and copying and pasting is the standard procedure, then.

-----------------------------------
dsantamassino
Tue Nov 09, 2004 10:33 pm


-----------------------------------
puts "John R.Doe?"
name = gets
puts "Hello " + John R.Doe + "!"

-----------------------------------
wtd
Tue Nov 09, 2004 10:41 pm


-----------------------------------
Ok, there are various steps to solving this problem.  


Ask for the person's first name
Get their first name and store it in a variable
Ask for their middle name
Get that name and store it in a variable
Ask for their last name
Store that name in a variable
Say hello, concatenating (adding) their first, middle, and last names into one name

The problem you're having with the code you pasted is that you don't quite seem to understand the purpose of a variable.  A variable is just a convenient name for a piece of data.  

The variable in the code you pasted was "name".  When you add together the strings in the last line of code, use that variable name.

puts "John R. Doe?"
name = gets
puts "Hello " + name + "!"

-----------------------------------
dsantamassino
Tue Nov 09, 2004 10:45 pm


-----------------------------------
Ok, there are various steps to solving this problem.  


Ask for the person's first name
Get their first name and store it in a variable
Ask for their middle name
Get that name and store it in a variable
Ask for their last name
Store that name in a variable
Say hello, concatenating (adding) their first, middle, and last names into one name

The problem you're having with the code you pasted is that you don't quite seem to understand the purpose of a variable.  A variable is just a convenient name for a piece of data.  

The variable in the code you pasted was "name".  When you add together the strings in the last line of code, use that variable name.

puts "John R. Doe?"
name = gets
puts "Hello " + name + "!"

I finally got it..

here is the output when i run ruby greet.rb

John R. Doe?

-----------------------------------
wtd
Tue Nov 09, 2004 10:47 pm


-----------------------------------
You'll have to type something and hit enter before the program should terminate.  :)

-----------------------------------
dsantamassino
Tue Nov 09, 2004 10:49 pm


-----------------------------------
You'll have to type something and hit enter before the program should terminate.  :)

OK i got it.

-----------------------------------
wtd
Tue Nov 09, 2004 10:59 pm


-----------------------------------
You'll have to type something and hit enter before the program should terminate.  :)

OK i got it.

Now, do you see what I was saying about "!" ending up on a new line?

-----------------------------------
dsantamassino
Wed Nov 10, 2004 6:28 am


-----------------------------------
You'll have to type something and hit enter before the program should terminate.  :)

OK i got it.

Now, do you see what I was saying about "!" ending up on a new line?

yeah i see what you was saying. well i have to go to work soon here. I will be back later.. so since i got that whats my next step??

-----------------------------------
wtd
Wed Nov 10, 2004 4:02 pm


-----------------------------------
Ask a user for their first, middle, and last names.  Then greet the user with their full name.

Example run:

C:\> ruby greet.rb
Your first name is?
John
Your middle name is?
Ryan
Your last name is?
Doe
Hello, John Ryan Doe!

-----------------------------------
jamonathin
Thu Mar 31, 2005 12:44 pm


-----------------------------------
I know you're probabily gonna think i'm retarded, but how do you run ruby? Like I started looking at your (wtd's) tutorial on puts and stuff, and i cant figure out how to run the thing :? . . . . .

-----------------------------------
Tony
Thu Mar 31, 2005 12:49 pm


-----------------------------------
irb works wonderfully. Actually look over [url=http://www.compsci.ca/v2/viewtopic.php?t=8181]this topic

-----------------------------------
jamonathin
Thu Mar 31, 2005 12:51 pm


-----------------------------------
Oh, duh!  Ok thanks.  :)

-----------------------------------
jamonathin
Thu Mar 31, 2005 3:36 pm


-----------------------------------
Ummm...  i have another question.  Do i use the SciTE to program? Because when i save it, it saves as like some unkown file type, not a ruby program.  But i installed it properly and everything because the sample programs work.  :?

-----------------------------------
wtd
Thu Mar 31, 2005 3:41 pm


-----------------------------------
Ummm...  i have another question.  Do i use the SciTE to program? Because when i save it, it saves as like some unkown file type, not a ruby program.  But i installed it properly and everything because the sample programs work.  :?

You can use any text editor you'd like.  Ruby programs are simply plain text with a ".rb" extension.

On Wndows I like TextPad.

-----------------------------------
Hikaru79
Sat Apr 02, 2005 7:04 pm


-----------------------------------
Jamonathin, if you want to use some sort of editor/IDE instead of compiling by hand (which is reccomended for now, but anyway), you should check out FreeRIDE. Works equally fine in Windows and Linux.
