Lesson 1, learning the basics of programming
As I’ve mentioned before, I’m not a programmer by trade or by nature. I don’t really think analytically, so trying to learn a programming language is really hard for me. That means that this section is very important. Learning the basic concepts will help build a foundation for when things get to be more complicated. Yes, I know that the previous sentence is the most obvious statement in the history of time, but it is at this stage that I usually screw up. I don’t take enough time to learn the terminology or to really understand the basics and then shortly after, I’m lost.
I should mention that I’m using Why’s (Poignant) Guide to Ruby for the first part of this adventure. Why (as in why the lucky stiff) is, as best I can tell, a mad genius. His project Try Ruby! made me realize that I could do this—that I could learn a programming language even though I’ve failed so many times in the past. His latest project Hackety Hack, which is designed to make programming acessible to kids (in the way BASIC was to us back in the 80’s), made me truly understand his brilliance. It also made me a huge fan. I would urge you to check out all these sites, but particularly the last one for it’s unrivaled awesomeness.
Okay, on to what I’ve learned:
The first section of the guide is meant to warm us up to the terminology and the basic functions of Ruby code. I’m going to make a list here so I have a quick reference for the future. I think I’m going to abbreviate the notes I took, because I don’t want this post to be insanely long or detailed. I just want a quick guide. If none of this makes sense, I would strongly urge you to visit Why’s (Poignant) Guide to Ruby. Again, this is only meant to be a reference (and maybe a study guide). Also, there were a few sections that I wasn’t 100% clear on the information being conveyed. I will make note of that anywhere it’s applicable.
- Variables → x, y, bananna2. Any plain, lowercase word in Ruby. Used as a nickname for something you use frequently. Syntax: teddy_bear_fee = 121.08
- Numbers → 1, 23, -10000. Basic number are called “integers” while decimals are called “floats.” Use underscores to make large numbers readable. Syntax: 12_000_000_000
- Strings → Any characters surrounded by quotes. Storing multiple characters with quotes causes them to be stored together as a single unit called a string. Single or Double quotes can be used. Syntax: avril_quote = “I’m a lot wiser. Now I know what the business is like.”
- Symbols → :a, :b, :ponce_de_leon. Look like variables, but they begin with a colon. Used as lightweight strings, usually stored where you need a string but won’t be printing to screen. Update: Symbols can be used multiple times, but only get stored once, so they are easier on the computer.
- Constants → Time, Array, Bunny_Lake_Is_Missing. Words like variables, but capitalized. They refer to something very specific and they don’t ever change. Syntax: EmpireStateBuilding = “350 5th Avenue, NYC, NY (because you can’t just move the Empire State Building!)
- Methods → If variables and constants are nouns, methods are verbs. They are usually attached to the end of variables or constants by a dot. They can be chained together. Syntax: front_door.open.close
- Method Arguments → Methods sometimes require more info to perform an action. The arguments are usually surrounded by parentheses and separated by commas. The following paints a door 3 coats of red paint. Syntax: front_door.paint( 3, :red )
- Class Methods → Usually attached after variables and constants. Rather than a dot, a double colon is used. Syntax: Door::new(
ak ) *need much more info about this one*
- Global Variables → $x, $y, $chunky. Variable that begins with dollar sign. Unlike most variables, which are temporary, global variable will always be the same. They can be used anywhere in your program.
- Instance Variables → @x, @y, @chunky_bacon. Variables that begin with the at symbol. Used to define attributes of something. For example, for front_door, we could set the width of the door with @width. Think of the at symbol as meaning attribute. *Need more info on this one*
- Class Variables → @@x, @@y. Variables that begin with double at symbols. They are used at a higher level. Think of @@ is as attribute all. *Need more info on this one too*
- Blocks → Any code surrounded by curly braces. Used for grouping a set of instructions so the code has become a single unit. Syntax: 2.times { print “something.” } Note: Curly braces can be exchanged for the words “do” and “end.” This works well when a block is longer than one line.
- Block Arguments → Set of variables surrounded by a pipe and separated by commas. They are always used at the beginning of a block. Syntax: { |x,y| x+y } *Need more info*
- Ranges → Two values surrounded by parentheses and separated by an ellipses (in the form of either two dots or three dots). Two dots returns the entire range, three dots causes the last value in the range to be excluded. Ranges can be numbers or letters. Syntax: (1..3) – provides a range from 1 through 3. (0…5) – provides a range from 1 through 4.
- Arrays → A list surrounded by square brackets, separated by commas. It is a collection of things in a specific order. Syntax: [1, 2, 3] or ['coat', 'mittens', 'snowboard']
- Hashes → Created by using the equals sign and the greater than sign. Matches a word with a definition. Hashes are easy to search through, and unlike arrays, items are not kept in a specific order. Syntax: { ‘a’ => ‘aardvark’, ‘b’ => ‘badger’}
- Regular Expressions → Sometimes called “regexp” is a set of characters surrounde by slashes. They are used to find words or patterns in text. Syntax: /ruby/ or /[0-9]+/ or /^\d{3}-\d{4}/
- Operators → Used to do math in Ruby, or to compare things.
** ! ~ * / % + - & << >> | ^ > >= < < <=> || != =~ !~ && += -= == === .. … not and or
- Keywords → Built in words that have meaning in Ruby. They can not be used as variables and they can not be changed.
alias
and
BEGIN
begin
break
case
class
def
defined
do
else
elseif
END
end
ensure
false
for
if
in
module next
nil
not
or
redo
rescue
retry
return
self
super
then
true
undef
unless
until
when
while
yield
Another point that Why makes is that Ruby language is much easier to read than traditional programming languages. In fact, in most cases, you should be able to read it aloud and have it make sense—which is very cool.
Okay, I think that’s enough for now. I hope this works as a handy little guide. I’m going to spend some time researching the few areas that I didn’t completely understand.
on August 3, 2007 on 7:39 pm
Great job, and thank you for the Why’s (Poignant) Guide to Ruby link.
on August 5, 2007 on 10:31 pm
Thanks Sebastien! And yeah… Why’s guide is just amazing. I hope you enjoy it as much as I have!