Sunteți pe pagina 1din 4

MANUAL RUBY

And numbers are different from strings. While you can use methods on any object in Ruby, some methods only work on certain types of things. But you can always convert between different types using Ruby's "to" methods.

to_s converts things to strings. to_i converts things to integers (numbers.) to_a converts things to arrays.

What are arrays?! They are lists. Type in a pair of brackets: []. You can't reverse the number forty. I guess you can hold your monitor up to the mirror, but reversing a number just doesn't make sense. Ruby has tossed an error message. Ruby is telling you there is no method reverse for numbers. Maybe if you turn it into a string: 40.to_s.reverse. Let's look at what you've learned in the first minute.

The prompt. Typing code into the prompt gives you an answer from a red prompt. All code gives an answer. Numbers and strings are Ruby's math and text objects. Methods. You've used English-language methods like reverse and symbolic methods like * (the multiplication method.) Methods are actions!

This is the essence of your learning. Taking simple things, toying with them and turning them into new things. Feeling comfortable yet? I promise you are. Okay, let's do something uncomfortable. Try reversing a number: 40.reverse Now, I'm sure by now you're wondering what any of this is good for. Well, I'm sure you've been to a website that screamed, Hey, your password is too short! See, some programs use this simple code. Watch this. Let's multiply your name by 5. "Jimmy" * 5

Counting the Letters


Objective:
You have used the reverse method on your name!

By enclosing your name in quotes, you made a string. Then you called the reverse method, which works on strings to flip all the letters backwards. Now, let's see how many letters are in your name: "Jimmy".length

Say Your Name Reversed


Objective:
Perfect, you've formed a string from the letters of your name. A string is a set of characters the computer can process. Imagine the letters are on a string of laundry line and the quotes are clothespins holding the ends. The quotes mark the beginning and end. To reverse your name, type: "Jimmy".reverse (Don't forget the dot!)

Say Your Name


Objective:
Sure, computers are handy and fast for math. Let's move on. Want to see your name reversed? Type your first name in quotes like this: "Jimmy" Great, that's an empty list. Lists store things in order. Like standing in line for popcorn. You are behind someone and you wouldn't dream of pushing them aside, right? And the guy behind you, you've got a close eye on him, right? Here's a list for you. Lottery numbers: [12, 47, 35]. A list of lottery numbers. Which one is the highest? Try: [12, 47, 35].max. Good, good. But it's annoying to have to retype that list, isn't it? Let's save our numbers inside a ticket like so: ticket = [12, 47, 35]

Summary #2 is Upon Us
Objective:

You had a list. You sorted the list. The ticket variable is now changed. Did you notice that the sort! method has a big, bright exclamation at the end? A lot of times Ruby methods shout like that if they change what the variable contains for good. It's nothin' special, just a mark. Now, look how your second minute went:

Errors. If you try to reverse a number or do anything fishy, Ruby will skip the prompt and tell you so. Arrays are lists for storing things in order. Variables save a thing and give it a name. You used the equals sign to do this. Like: ticket = [14, 37, 18].

In all there are eight lessons. You are two-eighths of the way there! This is simple stuff, don't you think? Good stuff up ahead. Let's change directions for a moment. I've stuffed a bit of poetry for you in a certain variable. Take a look. Type print poem Look, it's okay. You don't have to like it. Hack it up, be my guest. Instead of toast, go for a melon or something. Try this: poem['toast'] = 'honeydew'

Ready, Aim
Objective:
The square brackets you just used are very common in Ruby. Remember, you typed: poem['toast'] = 'honeydew'. That box with the word toast has a square bracket on each side, see? The two brackets are like sights used to line up a target. Exactly. These brackets mean, "I am looking for ____." Ready, aim. Here you're looking for toast and swapping it out with fruit. Here's a question: what happens when we reverse this whole poem? poem.reverse

Of All the Summaries, #3 is Here Now


Objective:
Good show, my friend! The join method took that list of reversed lines and put them together into a string. (Sure, you could have also just used to_s.) Review time.

Exclamations. Methods may have exclamations (and also question marks) in their name. No big deal. Try: poem.include? "my hand" Square brackets. Target and find things. Search and replace. Chaining methods lets you get a lot more done. Break up a poem, reverse it, reassemble it: poem.lines.to_a.reverse.join

At this point, you may want to tinker with the poem a bit more. A complete list of all the String methods is here. Go ahead and try a few (such as poem.downcase or poem.delete.) When you're ready to move on, type: books = {}

S-ar putea să vă placă și