Computer Science Canada

wtd, ruby questions

Author:  dsantamassino [ Thu Nov 04, 2004 10:56 pm ]
Post subject:  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 ??

Author:  Hikaru79 [ Thu Nov 04, 2004 11:01 pm ]
Post subject: 

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.

Author:  dsantamassino [ Thu Nov 04, 2004 11:03 pm ]
Post subject: 

Hikaru79 wrote:
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.

Author:  wtd [ Thu Nov 04, 2004 11:04 pm ]
Post subject: 

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.

Author:  wtd [ Thu Nov 04, 2004 11:05 pm ]
Post subject: 

dsantamassino wrote:
Hikaru79 wrote:
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. Smile

Author:  dsantamassino [ Thu Nov 04, 2004 11:08 pm ]
Post subject: 

wtd wrote:
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.

Author:  wtd [ Thu Nov 04, 2004 11:10 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
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...

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

Author:  dsantamassino [ Thu Nov 04, 2004 11:15 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
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...

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

Author:  wtd [ Thu Nov 04, 2004 11:20 pm ]
Post subject: 

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\

code:
C:\> E:

E:\> cd "Programming\Ruby"

E:\Programming\Ruby>


Now I'm in the right directory.

code:
E:\Programming\Ruby> ruby calc.rb

Author:  dsantamassino [ Thu Nov 04, 2004 11:25 pm ]
Post subject: 

wtd wrote:
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\

code:
C:\> E:

E:\> cd "Programming\Ruby"

E:\Programming\Ruby>


Now I'm in the right directory.

code:
E:\Programming\Ruby> ruby calc.rb


ok it worked. Now let me read some more of the tutorial..

Author:  wtd [ Thu Nov 04, 2004 11:29 pm ]
Post subject: 

Happy hacking. Smile

Author:  dsantamassino [ Fri Nov 05, 2004 6:18 pm ]
Post subject: 

My next set of Ruby questions will be in this topic. At the rate im going i probably wont get nothing done..

Author:  wtd [ Fri Nov 05, 2004 6:33 pm ]
Post subject: 

dsantamassino wrote:
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.

Author:  dsantamassino [ Fri Nov 05, 2004 6:41 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
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??

Author:  wtd [ Fri Nov 05, 2004 6:44 pm ]
Post subject: 

Fine with me. Just give me some time to reply. I'm writing an introduction/tutorial on Python's generators and iterators.

Author:  dsantamassino [ Fri Nov 05, 2004 6:48 pm ]
Post subject: 

wtd wrote:
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..

Author:  wtd [ Fri Nov 05, 2004 6:55 pm ]
Post subject: 

Ok... Numbers.

Any questions here?

Author:  dsantamassino [ Fri Nov 05, 2004 6:58 pm ]
Post subject: 

wtd wrote:
Ok... Numbers.

Any questions here?


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

Author:  wtd [ Fri Nov 05, 2004 6:59 pm ]
Post subject: 

Have you got irb open? If you do, you should see a prompt like:

code:
irb(main):001:0>

Author:  dsantamassino [ Fri Nov 05, 2004 7:01 pm ]
Post subject: 

wtd wrote:
Have you got irb open? If you do, you should see a prompt like:

code:
irb(main):001:0>


ok its open.

Author:  wtd [ Fri Nov 05, 2004 7:05 pm ]
Post subject: 

code:
irb(main):001:0> puts 1 + 2
3
=> nil
irb(main):002:0>


Do you see what's going on here?

Author:  dsantamassino [ Fri Nov 05, 2004 7:07 pm ]
Post subject: 

wtd wrote:
code:
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..

Author:  wtd [ Fri Nov 05, 2004 7:11 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
code:
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.

Author:  dsantamassino [ Fri Nov 05, 2004 7:13 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
code:
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

Author:  wtd [ Fri Nov 05, 2004 7:18 pm ]
Post subject: 

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.

Author:  dsantamassino [ Fri Nov 05, 2004 7:19 pm ]
Post subject: 

wtd wrote:
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??

Author:  wtd [ Fri Nov 05, 2004 7:28 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
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.

code:
irb(main):002:0> puts 5 * (12-8) + -15
5
=> nil
irb(main):003:0>


Do you understand what's going on here?

Author:  dsantamassino [ Fri Nov 05, 2004 7:33 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
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.

code:
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

Author:  wtd [ Fri Nov 05, 2004 7:38 pm ]
Post subject: 

I'm only 24, young'en. Wink

And congrats, but limit the profanity. We don't want school filters restricting access to this site. Smile

Author:  dsantamassino [ Fri Nov 05, 2004 7:39 pm ]
Post subject: 

wtd wrote:
I'm only 24, young'en. Wink

And congrats, but limit the profanity. We don't want school filters restricting access to this site. Smile


Ok sorry.

Author:  wtd [ Fri Nov 05, 2004 7:53 pm ]
Post subject: 

Now, write a line of code which displays the number of seconds in a year. Smile

Author:  dsantamassino [ Fri Nov 05, 2004 8:39 pm ]
Post subject: 

wtd wrote:
Now, write a line of code which displays the number of seconds in a year. Smile


No offense but are you on drugs?

my god. I dont know that much math or how to do it. go smoke another one.

Author:  JHanson90 [ Fri Nov 05, 2004 8:51 pm ]
Post subject: 

dsantamassino wrote:
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. Smile

Author:  wtd [ Fri Nov 05, 2004 8:51 pm ]
Post subject: 

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?

Author:  dsantamassino [ Fri Nov 05, 2004 9:05 pm ]
Post subject: 

wtd wrote:
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

Author:  dsantamassino [ Fri Nov 05, 2004 9:05 pm ]
Post subject: 

wtd wrote:
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

Author:  wtd [ Fri Nov 05, 2004 9:07 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
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)

Author:  dsantamassino [ Fri Nov 05, 2004 9:14 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
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??

Author:  wtd [ Fri Nov 05, 2004 9:23 pm ]
Post subject: 

Hint: note what happens when you run the following in irb.

code:
puts 5 * 4 * 2

Author:  dsantamassino [ Fri Nov 05, 2004 9:46 pm ]
Post subject: 

wtd wrote:
Hint: note what happens when you run the following in irb.

code:
puts 5 * 4 * 2


40
=> nil

Author:  wtd [ Fri Nov 05, 2004 9:51 pm ]
Post subject: 

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.

Author:  dsantamassino [ Fri Nov 05, 2004 9:53 pm ]
Post subject: 

oh ok..

yeah i understand and i see the pattern..

Author:  wtd [ Fri Nov 05, 2004 9:59 pm ]
Post subject: 

Good.

Moving on...

Do you know why 4 is different from 4.0?

Author:  dsantamassino [ Fri Nov 05, 2004 10:01 pm ]
Post subject: 

wtd wrote:
Good.

Moving on...

Do you know why 4 is different from 4.0?


Not really.

Author:  wtd [ Fri Nov 05, 2004 10:09 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
Good.

Moving on...

Do you know why 4 is different from 4.0?


Not really.


code:
4


Is an integer. It's a whole number.

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

code:
3 / 2


Is 1.

code:
3 / 2.0


Is 1.5.

Does this make sense?

Author:  dsantamassino [ Fri Nov 05, 2004 11:11 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
Good.

Moving on...

Do you know why 4 is different from 4.0?


Not really.


code:
4


Is an integer. It's a whole number.

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

code:
3 / 2


Is 1.

code:
3 / 2.0


Is 1.5.

Does this make sense?


Yes that makes sense.

Author:  wtd [ Fri Nov 05, 2004 11:15 pm ]
Post subject: 

Do you understand what a string is?

Author:  dsantamassino [ Fri Nov 05, 2004 11:17 pm ]
Post subject: 

wtd wrote:
Do you understand what a string is?


No. I heard of a string but dont know what it is.

Author:  wtd [ Fri Nov 05, 2004 11:21 pm ]
Post subject: 

A string is a sequence of characters.

code:
"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?

Author:  dsantamassino [ Sat Nov 06, 2004 10:53 am ]
Post subject: 

wtd wrote:
A string is a sequence of characters.

code:
"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..

Author:  wtd [ Sat Nov 06, 2004 3:09 pm ]
Post subject: 

On to string math.

You can use + and * on strings as well as numbers.

code:
"foo" + "bar"


is

code:
"foobar"


and, since...

code:
4 * 3


is just

code:
4 + 4 + 4


then

code:
"foo" * 3


is just

code:
"foo" + "foo" + "foo"


Which is

code:
"foofoofoo"


Any questions?

You should be trying these in irb as we go.

Author:  dsantamassino [ Sat Nov 06, 2004 6:53 pm ]
Post subject: 

wtd wrote:
On to string math.

You can use + and * on strings as well as numbers.

code:
"foo" + "bar"


is

code:
"foobar"


and, since...

code:
4 * 3


is just

code:
4 + 4 + 4


then

code:
"foo" * 3


is just

code:
"foo" + "foo" + "foo"


Which is

code:
"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??

Author:  wtd [ Sat Nov 06, 2004 7:05 pm ]
Post subject: 

Ah, you've struck on an interesting point.

code:
"3"


Is a string that contains the character 3.

code:
3


Is an actual number.

In something like:

code:
"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?

Author:  dsantamassino [ Sat Nov 06, 2004 7:23 pm ]
Post subject: 

wtd wrote:
Ah, you've struck on an interesting point.

code:
"3"


Is a string that contains the character 3.

code:
3


Is an actual number.

In something like:

code:
"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.

Author:  wtd [ Sat Nov 06, 2004 7:34 pm ]
Post subject: 

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

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

code:
"hello " * 3.5


Is

code:
"hello hello hello "

Author:  dsantamassino [ Sat Nov 06, 2004 7:39 pm ]
Post subject: 

wtd wrote:
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. Wink

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

code:
"hello " * 3.5


Is

code:
"hello hello hello "


oh ok i see.

Author:  wtd [ Sat Nov 06, 2004 7:54 pm ]
Post subject: 

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

code:
"4" * "3".to_i


Works just fine.

Or even...

code:
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. Smile

Author:  dsantamassino [ Sat Nov 06, 2004 9:08 pm ]
Post subject: 

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

Author:  wtd [ Sat Nov 06, 2004 9:15 pm ]
Post subject: 

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

code:
"4"


In your previous example, let's create a variable named "four" to stand for that value.

code:
four = "4"


Then, instead of:

code:
"4" * 3


We can write:

code:
four * 3

Author:  dsantamassino [ Sat Nov 06, 2004 9:17 pm ]
Post subject: 

Ok i understand that..

Author:  wtd [ Sat Nov 06, 2004 9:20 pm ]
Post subject: 

Just to make sure, you've been trying these on your own and seeing the right answers, right?

Author:  dsantamassino [ Sat Nov 06, 2004 9:23 pm ]
Post subject: 

wtd wrote:
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??

Author:  wtd [ Sat Nov 06, 2004 9:29 pm ]
Post subject: 

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

Author:  dsantamassino [ Sat Nov 06, 2004 9:31 pm ]
Post subject: 

right. alright i understand. Its pretty simple.

Author:  wtd [ Sat Nov 06, 2004 10:11 pm ]
Post subject: 

We've seen assignment to a variable. It looks like:

code:
son_of_integerificus = 42


Of course, we can assign more complex things to a variable.

code:
stringali_the_small = "foo" * son_of_integerificus


And there are shortcuts for assignments where we modify the variable.

code:
son_of_integerificus = son_of_integerificus * 2


Can become:

code:
son_of_integerificus *= 2


Similarly, we can do the same for +, -, /, and %.

Author:  dsantamassino [ Sat Nov 06, 2004 10:20 pm ]
Post subject: 

ok i wrote that in my irb..

Author:  wtd [ Sat Nov 06, 2004 10:22 pm ]
Post subject: 

Did you see what you expected to?

Author:  dsantamassino [ Sat Nov 06, 2004 10:27 pm ]
Post subject: 

wtd wrote:
Did you see what you expected to?


son_of_integerificus = son_of_integerificus * 2
=> 84

Is that right?? that was from irb.

Author:  wtd [ Sat Nov 06, 2004 10:37 pm ]
Post subject: 

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:

code:
puts 42


Is really:

code:
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?

Author:  dsantamassino [ Sat Nov 06, 2004 10:40 pm ]
Post subject: 

wtd wrote:
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:

code:
puts 42


Is really:

code:
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??

Author:  wtd [ Sat Nov 06, 2004 10:42 pm ]
Post subject: 

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:

code:
gets


And tell me what you see. Smile

Author:  dsantamassino [ Sat Nov 06, 2004 10:44 pm ]
Post subject: 

"\n"

Author:  wtd [ Sat Nov 06, 2004 10:48 pm ]
Post subject: 

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.

Author:  dsantamassino [ Sat Nov 06, 2004 10:49 pm ]
Post subject: 

I typed gets
dsantamassino
enter
"dsantamassino\n"

Author:  wtd [ Sat Nov 06, 2004 10:52 pm ]
Post subject: 

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?

Author:  dsantamassino [ Sat Nov 06, 2004 11:01 pm ]
Post subject: 

wtd wrote:
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.

Author:  wtd [ Sat Nov 06, 2004 11:06 pm ]
Post subject: 

Yep.

First off, save the following simple program in a file named "greet.rb".

code:
puts "Your name is?"
name = gets
puts "Hello, " + name + "!"


Remember where you saved it.

Author:  dsantamassino [ Sat Nov 06, 2004 11:10 pm ]
Post subject: 

ok i did save it.

Author:  wtd [ Sat Nov 06, 2004 11:12 pm ]
Post subject: 

Open up a command prompt. Start -> Run, type "cmd" and hit enter.

Now, get to the drive where you saved the file.

Author:  dsantamassino [ Sat Nov 06, 2004 11:28 pm ]
Post subject: 

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

Author:  wtd [ Sat Nov 06, 2004 11:33 pm ]
Post subject: 

Did you save the file at the root of your C drive?

Author:  dsantamassino [ Sat Nov 06, 2004 11:34 pm ]
Post subject: 

Yes

wtd wrote:
Did you save the file at the root of your C drive?

Author:  wtd [ Sat Nov 06, 2004 11:40 pm ]
Post subject: 

Type "dir /B" and hit enter. See if greet.rb is in that list.

Author:  dsantamassino [ Sun Nov 07, 2004 7:06 am ]
Post subject: 

No its not there.

Author:  wtd [ Sun Nov 07, 2004 5:06 pm ]
Post subject: 

I thought not.

You'll need to "cd" to the directory (or "folder") greet.rb is in. The basic format is:

code:
C:\> cd "the folder\the file\is in"

Author:  dsantamassino [ Sun Nov 07, 2004 6:09 pm ]
Post subject: 

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

Author:  Hikaru79 [ Sun Nov 07, 2004 10:33 pm ]
Post subject: 

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:

code:
cd c:
cd Documents
cd Programming
cd Ruby
ruby greet.rb


Or, if you're feeling adventurous,

code:
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

Author:  dsantamassino [ Sun Nov 07, 2004 10:48 pm ]
Post subject: 

Hikaru79 wrote:
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:

code:
cd c:
cd Documents
cd Programming
cd Ruby
ruby greet.rb


Or, if you're feeling adventurous,

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

Author:  wtd [ Mon Nov 08, 2004 12:29 am ]
Post subject: 

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.

Author:  dsantamassino [ Mon Nov 08, 2004 6:29 pm ]
Post subject: 

wtd wrote:
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:\

Author:  wtd [ Mon Nov 08, 2004 7:12 pm ]
Post subject: 

Is it really "greet.rb", or did it get saved as "greet.rb.txt"?

Author:  dsantamassino [ Mon Nov 08, 2004 7:43 pm ]
Post subject: 

wtd wrote:
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

Author:  wtd [ Mon Nov 08, 2004 8:28 pm ]
Post subject: 

Ok... not gonna give up on this.

Try "type greet.rb" at the command-line. The Windows command prompt, not the IRB prompt.

Author:  dsantamassino [ Mon Nov 08, 2004 8:32 pm ]
Post subject: 

wtd wrote:
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.....

Author:  wtd [ Mon Nov 08, 2004 8:46 pm ]
Post subject: 

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:

code:
C:\> edit greet.rb


You'll see a blue editor screen. Here you can type in the program again, unless it's already there. Wink

code:
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:

code:
C:\> ruby greet.rb

Author:  dsantamassino [ Mon Nov 08, 2004 9:34 pm ]
Post subject: 

ok.. here is a different error message down below..

greet.rb:3: syntax error
puts "Hello, " + Derek "!"

Author:  wtd [ Mon Nov 08, 2004 9:43 pm ]
Post subject: 

dsantamassino wrote:
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". Smile

Author:  dsantamassino [ Mon Nov 08, 2004 9:48 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
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". Smile


Cool it worked.
Derek?

Author:  wtd [ Mon Nov 08, 2004 10:41 pm ]
Post subject: 

Spiffy.

Now, do you understand why the program went something like:

code:
C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!

C:\>


When it should have looked like:

code:
C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!

C:\>

Author:  dsantamassino [ Mon Nov 08, 2004 10:44 pm ]
Post subject: 

wtd wrote:
Spiffy.

Now, do you understand why the program went something like:

code:
C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek
!

C:\>


When it should have looked like:

code:
C:\> ruby greet.rb
And your name is?
Derek
Hello, Derek!

C:\>


No i dont know. May u explain it to me??

Author:  wtd [ Mon Nov 08, 2004 10:48 pm ]
Post subject: 

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.

code:
puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!"

Author:  dsantamassino [ Mon Nov 08, 2004 10:50 pm ]
Post subject: 

wtd wrote:
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.

code:
puts "And your name is?"
name = gets.chomp
puts "Hello, " + name + "!"


Oh ok i think i got it

Author:  wtd [ Mon Nov 08, 2004 11:09 pm ]
Post subject: 

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.

Author:  dsantamassino [ Tue Nov 09, 2004 5:48 pm ]
Post subject: 

I tired to do it but its pointed an upward error to the middle name

Author:  wtd [ Tue Nov 09, 2004 6:20 pm ]
Post subject: 

Post the code you write. Please surround it with code tags, like so:

code:
[code]This is formatted as code.[/code]

Author:  dsantamassino [ Tue Nov 09, 2004 6:38 pm ]
Post subject: 

wtd wrote:
Post the code you write. Please surround it with code tags, like so:

code:
[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

Author:  wtd [ Tue Nov 09, 2004 6:43 pm ]
Post subject: 

Copy and paste your code into a post. At the beginning of your code, type:

code:
[code ]


And at the end of it, type:

code:
[/code ]


Just don't use spaces between the e and ].

Author:  dsantamassino [ Tue Nov 09, 2004 7:08 pm ]
Post subject: 

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

Author:  wtd [ Tue Nov 09, 2004 7:15 pm ]
Post subject: 

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.

Author:  dsantamassino [ Tue Nov 09, 2004 9:59 pm ]
Post subject: 

wtd wrote:
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..

Author:  wtd [ Tue Nov 09, 2004 10:10 pm ]
Post subject: 

dsantamassino wrote:
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?

Author:  dsantamassino [ Tue Nov 09, 2004 10:15 pm ]
Post subject: 

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

Author:  wtd [ Tue Nov 09, 2004 10:18 pm ]
Post subject: 

dsantamassino wrote:
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".

Author:  dsantamassino [ Tue Nov 09, 2004 10:21 pm ]
Post subject: 

wtd wrote:
dsantamassino wrote:
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

Author:  wtd [ Tue Nov 09, 2004 10:30 pm ]
Post subject: 

Ok, stick with me, I think I can help with one last idea.

code:
C:\> notepad greet.rb


That should open the file in a new Notepad window, and copying and pasting is the standard procedure, then.

Author:  dsantamassino [ Tue Nov 09, 2004 10:33 pm ]
Post subject: 

code:
puts "John R.Doe?"
name = gets
puts "Hello " + John R.Doe + "!"

Author:  wtd [ Tue Nov 09, 2004 10:41 pm ]
Post subject: 

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.

code:
puts "John R. Doe?"
name = gets
puts "Hello " + name + "!"

Author:  dsantamassino [ Tue Nov 09, 2004 10:45 pm ]
Post subject: 

wtd wrote:
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.

code:
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?

Author:  wtd [ Tue Nov 09, 2004 10:47 pm ]
Post subject: 

You'll have to type something and hit enter before the program should terminate. Smile

Author:  dsantamassino [ Tue Nov 09, 2004 10:49 pm ]
Post subject: 

wtd wrote:
You'll have to type something and hit enter before the program should terminate. Smile


OK i got it.

Author:  wtd [ Tue Nov 09, 2004 10:59 pm ]
Post subject: 

dsantamassino wrote:
wtd wrote:
You'll have to type something and hit enter before the program should terminate. Smile


OK i got it.


Now, do you see what I was saying about "!" ending up on a new line?

Author:  dsantamassino [ Wed Nov 10, 2004 6:28 am ]
Post subject: 

wtd wrote:
dsantamassino wrote:
wtd wrote:
You'll have to type something and hit enter before the program should terminate. Smile


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??

Author:  wtd [ Wed Nov 10, 2004 4:02 pm ]
Post subject: 

Ask a user for their first, middle, and last names. Then greet the user with their full name.

Example run:

code:
C:\> ruby greet.rb
Your first name is?
John
Your middle name is?
Ryan
Your last name is?
Doe
Hello, John Ryan Doe!

Author:  jamonathin [ Thu Mar 31, 2005 12:44 pm ]
Post subject: 

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

Author:  Tony [ Thu Mar 31, 2005 12:49 pm ]
Post subject: 

irb works wonderfully. Actually look over this topic

Author:  jamonathin [ Thu Mar 31, 2005 12:51 pm ]
Post subject: 

Oh, duh! Ok thanks. Smile

Author:  jamonathin [ Thu Mar 31, 2005 3:36 pm ]
Post subject: 

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

Author:  wtd [ Thu Mar 31, 2005 3:41 pm ]
Post subject: 

jamonathin wrote:
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. Confused


You can use any text editor you'd like. Ruby programs are simply plain text with a ".rb" extension.

On Wndows I like TextPad.

Author:  Hikaru79 [ Sat Apr 02, 2005 7:04 pm ]
Post subject: 

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.


: