Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Ruby Specifics

  • This lesson will go through ruby syntax and common code conventions that are often asked by a new Ruby developer.
  • Comments are creating using '#'
  • Camel Case should be reserved only for classes
  • Parentheses are optional for functions
  • Newlines are statement terminators
  • You can continue method calling using a period
  • Multiple statements are allowed on a single line if separated using a semi-colon ";"

Ruby Specifics

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Ruby Specifics 0:06
  • Comments 0:51
    • Hashtags
    • Example
  • Multi-Line Comment 2:04
    • Example
  • RDoc Comments 4:02
    • When do you generate an Rdoc?
    • Headings and subheadings
    • Examples
    • Generating an Rdoc - example
  • Common Code Conventions 6:28
    • For every tab use two spaces indentation
    • Never use tabs
  • Common Code Conventions (Cont.) 8:18
    • Camel case
    • Snake case
  • Identifiers 9:44
    • Constants begin with CAP letter
    • Examples
    • Identifiers with Different Scoping
    • Global
    • Instance Variable
    • Class Variable
    • Examples
  • Reserved Keywords 12:22
    • Do not use reserved keywords in code
  • Parentheses are Sometimes Optional 13:04
    • Functions do not require parentheses
    • When in doubt, use parentheses
    • Examples
  • Newlines Are Statement Terminators 14:20
    • Examples
  • Continuation with a Period 16:20
    • Period means continue to next line
  • Multiple Statements Allowed on a Single Line 17:38
    • Try not to use semi-colons
  • Code Blocks 18:20
    • Use code blocks for one liners
    • Examples
    • Recommended for multiple lines

Transcription: Ruby Specifics

Welcome to

This site is on Ruby specifics.0001

For this course, we want to go over a lot of the common things you will see in Ruby--a lot of things people see as questions, and a lot of specifics that are just on Ruby itself.0006

First, we are going to go over the Ruby syntax.0022

There are a lot of things about Ruby that are different versus other languages, that you should know.0026

Hopefully, we will cover them here; and the second thing we are going to go over is the common code conventions.0032

The reason is that there are a lot of conventions in Ruby that you wouldn't see in other languages.0038

They are a lot more different, and we will go over it now.0045

The first thing we want to go over is comments.0051

They are created using the hashtag.0055

If you use other languages, you might see a forward slash--that's in PHP.0058

It is similar in Javascript; but in Ruby, they are hashtags.0067

What happens is, wherever the comment starts, it is going to continue to the end of that line.0073

For example, here on this slide, you have x=100.0082

You have this hashtag here, and then you have the actual comment you leave here.0087

You will notice that anything afterwards, it will pick it up, all the way to the new line over there, and the same over here.0093

The actual Ruby code looks at x=100 and y=200, and then it will use that for its Ruby compilation and whatever process they're parsing.0104

And here, on this site, we are showing you a multiple-line comment. This is one of the most common ways you will be commenting when you are doing multiple lines.0117

I would recommend you use this one here.0131

You will notice, on this line, there are four hashtags.0137

This one is a two-liner, and you will notice that there is a space here, and a space here.0141

That is very common. If you leave a comment, you always need some kind of spacing here.0149

If you have more to this comment, you could just add another hashtag.0155

In this case, if it is a paragraph, I leave one hashtag, and another hashtag here.0160

And then, I put whatever code or code description I have.0167

Here is another multi-line comment.0183

This one is not as popular; you will see it more often in other languages.0189

It has docstring type comment.0194

It starts with this begin tag; that is when it tells Ruby, "OK, if you see this =begin, don't actually do anything with it--don't interpret it; instead, take this as a comment."0197

And you will notice that everything all the way from begin until it says end--that is all a comment.0210

So, the main thing here is =begin and =end.0226

That will create this type of comment; I wouldn't recommend it--it is good to know, though, if you are transitioning from one language to another.0231

For this example, I am going to show you an RDoc comment.0240

The reason we are going over this one is to show you, when you generate your RDoc, you can have headers that are larger and smaller, depending on the topic.0246

If it's a main heading versus a subheading, you will notice here that you have this one equals sign, which is the main heading.0258

For a subheading, you will notice that there are two equals signs.0268

And, you could continue that down--so it becomes a sub-subheading with equal equal.0272

You will notice that I actually have some RDoc comments here.0286

First, you will see, with "Overview," there is =Overview.0290

That is a heading, and then you will see =Usage--there is another heading.0296

Here is ==Options--there is a subheading.0302

And For Future Use--that is another subheading.0307

Let's go ahead and generate this out and see what it looks like.0310

Again, we are in the current directory.0321

There is the hello-world.rb--I'm just going to run rdoc there.0324

It is going to generate it into this Doc directory.0329

I'm going to open the index file.0334

And the one we are looking at is this myfirstclass.0342

So, you can see from this browser, Overview is part of the RDoc--you know that it is a very huge text piece.0346

Usage is our heading.0355

Options is our subheading, and For Future Use is our sub-subheading.0358

You will notice the size difference.0361

Let's minimize that...0367

The next one I want to go over, the second part, is common code conventions.0387

What we want to do here is to go over some of the most popular conventions that you need to know for Ruby, and a lot of things you will notice that are very questionable, that we hope to answer here.0392

Here is a very basic code piece.0406

The class is Book; we have an attribute assessor for title, author, description, content.0411

This is our constructor; takes four arguments: title, author, description, content.0418

We create class lists and variables here.0425

We have a method called Summary; it creates a description, and that becomes a summary, because we get the first 150 characters.0428

And then, we have another method called Wordcount, and that counts the words from the content.0438

If you will get it all in...we will go into it in deeper parts in other lessons, too.0444

The first code convention is that you will use two spaces per indent.0451

And you will never use tabs.0458

That is very common. You will notice here that this is actually two spaces.0463

If you did have extra code, where it was a method and then you had an if statement; there would be another two spaces.0475

For every comment you have, you want to use two spaces indentation.0484

And also, you will notice at this class itself, the class is actually CamelCase.0496

But it is only for the class itself.0508

So we have the book--it's Capital--but then you will notice all the other methods here.0512

They are all snake_case, so they are all in this lowercase space format.0518

For an example of CamelCase, we could say, maybe, our next method could be BookTitle.0525

So, this would be an example of CamelCase.0539

But if I am making a method--let's say I make a method for BookTitle--you will never see capital letters; it will all be lowercase.0547

This is snake_case...let's call it that...0555

Let's call it get_title.0567

The next one we want to go over is identifiers.0582

You will use this all the time in coding.0585

They are very basic: you have your normal variables--your variable x, your variable temp--0588

You will also see constants.0596

Ruby convention for constants is that it always begins with a capital letter.0599

The recommended way is to keep it all caps, all the way.0604

So we have PI, all caps, MAX_WITH, all caps--between spaces, you use underscore.0610

And, in that same case, if you have different identifiers that are for different scopes...0624

Global has a dollar sign.0629

Instance variables have the "@" sign.0632

And then, for class variables, you have your "@@".0636

I want to show you an example of this using our Book code.0642

Here is our Book class example.0648

First, notice, we have our two spaces.0655

And then, for our method, we also have two spaces here.0658

You will notice our instance variables and our class...they have the "at" sign.0662

But let's say I wanted to add a class instance variable.0670

We can call it publisher.0679

Let's just call it Educator, LLC.0682

Let's say that I want to add that to my summary, so people will know that this book it published by Educator, LLC.0687

I can add this to my summary.0695

What that would display is...when you call up this summary method, it will show Educator, LLC and your description here.0705

Now, you have different reserved keywords in Ruby.0738

You want to make sure you are not using them in your code.0742

There is alias, and, begin, breakcase, class...def is a method, break is if you are in a loop, begin...and I'm sure you have used those--they are big caps, but more commonly you might use any of these words for a name.0744

Those are reserved, so if you can't find--your code is making a hiccup, or you have some kind of issue, it could be because it's one of these words in your code.0766

The next thing is: parentheses are optional.0778

They are sometimes optional, depending on how you look at it.0785

You do not need parentheses in your methods.0788

Functions do not require parentheses.0792

So, here for our method, search for page--there are no parentheses here.0799

But you will notice, here, we do use parentheses; they are actually the exact same method, but they are optional.0806

Here we have a search for page.0815

This is us calling the method; again, no parentheses.0819

My recommendation is, use parentheses if you have questions about it; once you get more flexible and know what you are doing, then you can remove them.0824

Most people coming from other languages actually enjoy using parentheses, and they are known to keep it that way.0834

So, I would suggest just to go with that tactic at the beginning.0840

And here is another example without parentheses: this is a very popular one--we are going to use puts a lot, and notice we don't need the parentheses here--it makes it easier.0845

For this library, to go over the new lines, these are actually statement terminators.0860

We don't have semicolons in the actual code itself.0870

Usually, in other pieces of code, you have a semicolon here that ends the statement.0874

But, in Ruby, it's common to just have the codeline.0880

The new line itself will actually end it.0885

For example, here you have total=x+y.0891

And you will notice that the x doesn't have any semicolon.0895

So x+y is not going to work at all.0903

Let's do an example with the IRB.0911

So, I do total=x+y, which is what we are going to do.0914

Let's do x=100, y=250.0923

That didn't seem to work. Let's try it again.0929


x=100, y=250, total=x+y.0945

So we get our 350; but you notice if I do total=x, it's already returned 100, and if I put +y, then I get 250, and if I put total itself, it's still going to declare just for the x-value.0955

The next thing is, you can continue code pieces with the period.0980

You would think--we just said--new lines are terminators: they end the statement; but in this case they're not.0985

This period tells it, "Hey, continue looking at the next line."0991

We are calling this array new, but it's also pushing introduction to this array.0998

And then we are calling this period...this period says, "OK, we're not ending it; we're going to continue to the next line."1004

Push your body, push down the array, push summary to array.1010

We have this array here that has introduction, we have our body, and we have summary.1016

And you will notice it's very clean--it never ended--it was actually one stem in itself, and it did the job.1047

For this sample--you can still use semicolons.1058

If you like semicolons, declare them, but get used to not using semicolons.1061

We can use multiple statements on a single line with it.1069

For example--this one is broken up into two lines, but if they were a single line, I would be doing two processes on one line.1075

I recommend you don't use this, though.1082

But there are cases where it is useful.1087

I'll show it to you on this next slide.1092

For example, if we have this code block, notice this is all one line.1100

That might not be exactly the same one I want to show you, though.1109

Let's go back to our Ruby Interpreter.1114

Let's say I'm doing a process, declaring a value to x, a value to y, and then I want to do total=x+y.1117

Notice, I want to show you that even when I enter the semicolon here, it does continue to the next line down.1135

So it hasn't ended the actual statement yet.1144

I can actually do puts total.1147

Notice that was all one line.1151

It outputs that for me.1155

Again, if I were to just do x=100, it's still going, so I can output it here.1158

The next part is code blocks.1173

I recommend you only use the one-liner for this one in cases where you have a single statement.1176

You will notice, for this one, I have a bracket, and a bracket here.1184

But the most recommended way to do it is with this "do" and the "end."1188

The reason is it's just much cleaner; notice, when I use these brackets, I can put it all in one line, which is nice.1196

But, if you have multiple statements, you want to use the do x, your put xx, your other statements, and your end.1205

This one here, I would say, is recommended for multiple lines.1215

And with that, we have gone through our Ruby specifics course.1235

Thank you for being with me here, and I will see you in the next one.1239