- A string is a set of characters — letters, numbers, symbols, punctuation
- You can add strings together (don’t forget to add a space!)
- You can multiply a string by a number
- You cannot add a string and a number, or multiply strings together or you’ll get an error like:
TypeError: can't convert Fixnum into String
TypeError: can't convert String into Integer
- Use a backslash to escape an apostrophe or a backslash
That’s about it. Cool beans, let’s move on.
I’ve been remiss in my posting duties…I am halfway through Chris Pine’s book and just now taking a look back to post my notes from the previous chapters. Notes will be short; I want to focus on the programming!
- I always wondered what a “float” really was. Chris Pine doesn’t really define it; but my understanding is that it’s really just a bunch of decimal points (as opposed to an integer).
- Ruby programs will add numbers together just like a calculator. Don’t use commas, though.
- If you calculate two floats together, you’ll get the exact answer in a float. If you calculate two integers, and the answer isn’t an integer, Ruby always rounds down. Good to know.
- More complex expressions can include parentheses to keep things straight.
Exercises
1. How many hours are in a year?
puts 24*365
2. How many minutes are in a decade?
puts 60*24*365*10
3. Calculate your age in seconds.
puts 29*365*24*60*60
4. If I am 1,025 million seconds old, how old am I?
puts 1025000000/60/60/24/365
So I’m learning to program. I have a background in design and front-end development, focusing mostly on HTML & CSS, usability, and just enough Ruby on Rails to get my job done. I happen to work with a lot of fantastic Ruby and Rails developers, and decided that I’d like to take my web development skills to the next level.
I’m beginning with the book Learn to Program, by Chris Pine. It teaches fundamental programming concepts using Ruby as an example.
As I go through the book, I’d like to post my notes and the answers to the exercises. Partly so I have a record of my progress, and partly to share with others who are learning too.
Enjoy!