More Fun With Refactoring - Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

More Fun With Refactoring

Typing an array with quotes and commas can be time consuming. There is a shorter syntax.

ary = %w[name age gender]

print ary

["name", "age", "gender"]

The percent sign followed by a w indicate to Ruby that this will be an array of strings separated by whitespace.

We've decided to add "height" to our array. Recall that we can do this by using push.

ary.push("height")

print ary

["name", "age", "gender", "height"]

Let's use an alternative of push to add weight to our array.

ary << "weight"

print ary

["name", "age", "gender", "height", "weight"]

Classes

Ruby is an object oriented language. That means almost everything is an object. An array is an object. A string is an object. All these objects belong to a class. A string belongs to the String class. An array belongs to the Array class. We can check an object's class.

["here", "I", "am"].class.name

Array

"here I am".class.name

String

We're going to create our own class called Warrior. Class names are written in CamelCase. Which means they can go a long time without water. [crickets] It actually means we separate words using capital letters rather than underscores. The capital letters make the words look like they HaveHumps. Our class is only one word. We still need to start it with a capital letter. Take a look at the Warrior class before I break it down. Familiarize yourself a bit.

class Warrior

def initialize(name, clan, strength, dexterity)

@name = name

@clan = clan

@strength = strength

@dexterity = dexterity

end

def about

puts "#{@name} is a warrior from the #{@clan} clan. Strength is #{@strength}. Dexterity is #{@dexterity}."

end

end

On line 2 you'll see a method called initialize. This is our proverbial teacher slapping the books down on the desk to wake a sleeping student. "Wake up object!" Then we gave the method four parameters: (name, clan, strength, dexterity)

On lines 3-6 we created variables and set them equal to our parameters. Notice these variables all started with @ symbols. This means they're instance variables. It means each object that's in this class will have their own. Meaning: The @strength value for one object is different from the @strength for another.

After creating our instance variables we created another method that's called about. When we call this about on our object it will puts out the string that tells us all about our warrior. Take another look at the class. At the end of it we will create a new Warrior object called player_1.

class Warrior

def initialize(name, clan, strength, dexterity)

@name = name

@clan = clan

@strength = strength

@dexterity = dexterity

end

def about

puts "#{@name} is a warrior from the #{@clan} clan. Strength is #{@strength}. Dexterity is #{@dexterity}."

end

end

player_1 = Warrior.new("Raze","Fox",23,34)

We can call the method about like we would with other methods.

player_1.about

Raze is a warrior from the Fox clan. Strength is 23. Dexterity is 34.

To create an object in a certain class you set a variable equal to ClassName.new(para1,para2,para3,para4) Let's create another Warrior object.

player_2 = Warrior.new("Bazerk","Orc",17,25)

player_2.about

Bazerk is a warrior from the Orc clan. Strength is 17. Dexterity is 25.

Global Variables

These variables have big ears. You can call them from far away and they'll hear you. You can call them when they're in classes or methods. To name a global variable you start it with a $ symbol. In your Warrior class, create a global variable called $warrior_info and make it equal a string with a basic description of a warrior.

$warrior = "A fighter. A warrior has higher than normal strength. Warriors rely on their instinct and experience in the face of battle."

Now go to the bottom of your file, outside of your class, and type:

puts $warrior.

A fighter. A warrior has higher than normal strength. Warriors rely on their instinct and experience in the face of battle.