Extra Credit:
How do I put a variable in a string?
Declare a variable called myname and put my name the string into it.
>> name = "Stayce"
=> "Stayce"
It looks like Ruby understood me, but let's double check
>> name
=> "Stayce"
But now we need to learn some funny characters to be able to make ruby understand how to tell the difference between our variable called name (which is set to Stayce) and the rest of the text in a string. The characters ruby looks for to know how to interpret a variable first, then print the string is #{}.
So the Ruby program is running along, listening to your commands, and when it sees a "quote it knows a string is coming. Ruby prints out each character including spaces, then it sees a #hash followed by a {curly-bracket! Ruby stops reading the string, instead it looks up the value of your variable, finds out it's a string, prints that and then goes back to printing the rest of your string until it reaches the close quotes". Ruby's smart, but it can't read minds, so you have to do it this way.
Anyway, here's how to use those characters so you can print variables inside your strings.
>>"My name is #{name}"
=> "My name is Stayce"
Some other interesting classes are Time and Date. Time has some methods you will use often. Let's check out the Time class and use the .now method.
>>Time.now
=> Sun May 16 23:22:27 -0700 2010
Cool, Ruby tells time. What about the Date class? The .now method doesn't work on the Date class, but the .today method does.
>> Date.today
=> #
That looks like a computer date. Let's see it in English using the .to_s method
>> Date.today.to_s
=> "2010-05-16"
So what are these Time and Date classes? They are classes of their own. Dates and Times are stored differently than Fixnums and Strings and Floats. They are important enough to have their own sets of methods, so they have their own class. But what class does their class belong to?
>> Date.class
=> Class
wtf?! Date belongs to the Class class? What does that even mean? Classes can belong to other classes so they can inherit their parent classes characteristics. That way you don't have to repeat yourself if you have similar "child classes". You can think of why this makes sense if you think of a real world example like classifying animals. Mammals inherit from the Animal class, Dogs inherit from the Mammal class, PitBulls inherit from the Dog class. So when you add Fido to the PitBull class, you don't need to specify that he has four legs, fur, warm blood, and needs to be walked. All of those characteristics would be inherited from the parent classes Mammal and Dog. Whew! That was a lot to explain, but what about the Class class? "All metaclasses are instances of the class `Class’. " That's better explained here: Class: Class.
Symbols - Symbols are another class of objects you use a lot in Ruby on Rails. Before we talk about what Ruby on Rails even is, let's make a couple of symbols to see what they look like.
Symbols are similar to strings but they do not inherit from the String class. I'm jumping ahead of myself here, but if you bother to decipher that last sentence you can maybe guess that classes can inherit properties from other classes;)
You create a symbol using the colon, then name it like this >>:dog
You can tell it's in the symbol class by appending .class and checking what irb returns.
>> :dog.class
=> Symbol
Why you would want use symbols and what they really are is coming up later. Oh, you really want to know now? Are you sure? Just skip ahead if you don't care yet. (Click to show/hide)
Well, in RoR (that's ruby on rails) you often use symbols as:
keys for hashes or to pass named arguments to methods
User.find(:all, :conditions => "admin = true")
as flags for state values
user = User.new
user.role = :admin
To represent other classes in Active Record
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :group
has_many :friends
That's a peek at some ruby on rails code. You can't run that code in your irb yet because we haven't talked about data yet. Programming usually involves storing data...not just typing stuff in a terminal that gets deleted when you close the window. We'll get to data later. Let's keep learning about Ruby.
Lesson 5 -
Instance Variables
Earlier we made variables by typing a word then the =equals sign then a string in "quotes >> firstname = "Stayce" or a number >> age = .