Giter VIP home page Giter VIP logo

ruby-course's People

Contributors

profgs avatar steven-galvin avatar

Watchers

 avatar

ruby-course's Issues

Set up your environment

Hey there!

Today we will be learning the basics of Ruby. Ruby is a flexible and dynamic language, developed to feel natural and simple (some even call it beautiful๐Ÿ’Ž). It is open source, and widely popular with the Rails framework.

Today, I'm going to guide you through your first Ruby project. First, we will write a program to output "Hello world" (as software developers are known to do). Then, we will learn some ruby methods to collect user input. Lastly, we will display different statements based on their input.

This is an interactive course, where you will be prompted to do something to finish each step. If it takes more than a few seconds for a response, try refreshing your browser.

Leave a comment with your name to continue

Using If/Else Statements

Using If Statements for Conditional Execution

Being able to output strings and collect inputs is certainly useful, though being able to check conditions and execute specific code according to those conditions is where the real power of development begins. If Statements allow us the ability to check if something is true, and if that something is true, then a block of code will run.

To get us started, let's open up the file named montys-color.rb in our text editor and we'll create a program similar to our last project. We're going to prompt the user for their favorite color, assign that input to a variable, and then output a string containing the result of that input.

In lesson three, uncomment lines three, four, and five and then you should have the following:

puts "What is your favorite color?"
color = gets.chop
puts "Wow! My favorite color is #{color} also!"

If you save the file and run it in the terminal, you'll see that it works like our first project.

But what if your favorite color isn't the same as the program's? What if we want our program to only output that response if the user inputs blue as their favorite color? That's where If statements come into play.

We're going to modify our code to create an If statement that does just that, but first, commit and push the changes you've made to the repo.

With the project backed up, let's create put that output into an If statement. Let's comment out line five by putting the # back onto the front of the code and uncomment lines nine, ten, and eleven in lesson four. You should have the following code:

if color == "blue"
    puts "Wow! My favorite color is #{color} also!"
end

What's nice about Ruby is that it prioritizes readability because it's written like plain English. This code is saying, "If color is equal to blue, then put "Wow! My favorite color is blue also!"

If statements evaluate for true or false. If the expression that follows If is true, then it means the condition is met and the code between If and end runs. If the expression is false, the code does not run.

We use == to check if the two values are equal. Since a single equal sign is used to set a value, we use two equal signs to compare values.

End indicates the end of the If statement.

If we run this code in the terminal, and enter "blue" in the prompt, we will see our string printed on the screen. If we enter anything else, our string will not print. But what if we want something different to print even when the input isn't "blue?"

Commit and push the changes you've made to find out how.

git add .
git commit -m"add if statement"
git push

Run a 'hello world' program

Writing Our First Program: "Hello World!"

As I mentioned earlier, "Hello world" is a staple of web development. It's a program that every new developer will write at least a dozen times, and we're going to start by creating our own "Hello world" today.

To write "Hello world," let's start by navigating into our cloned repository by typing the following:

cd ruby-course

After navigating into our project folder, open up the file hello.rb and check out the code. There will be two lessons in this file, let's start by looking at lesson one.

In lesson one, you'll see the following line of code:

# puts "Hello World!"

What we have here is the Ruby method, puts, and the string, "Hello World!".

The # symbol turns our line of code into a comment which means that it won't be read when we run our program. It's there only for us to read. In order to turn a line of code into a comment in Ruby, the # must be placed at the start of a line of code.

We want this line to run, so let's delete the # and then save our file.

In programming, you'll often have pieces of code that need to be executed many times. Methods are a way to store that code in one place and then if you need to execute that code later, you can just call the method instead of writing out your block of code all over again.

Ruby comes with some default methods like our puts above, and these methods are available right away when you create Ruby projects. You can also create your own methods, though we aren't going to be getting into that in this tutorial.

A string is simply a line of text. They can be short like the title of this article, or long like an entire paragraph. The important thing to know is that strings are defined by enclosing text within single or double quotes.

The puts method tells our computer to print something to the screen. That something, in this case, is the string "Hello World!".

Leave a comment with correct letter that answers this question:

Which statement is true about Ruby methods?
a. Methods are lines of text.
b. You always need to write a method before using it.
c. They are pieces of code (default or custom) that can be reused.

Using Ruby Methods

Prompting Us for Input

Knowing how to print strings is great, but it can get stale printing the same output over and over again. The next concept we are going to learn is how to make our program address us by name! We can do that by prompting for user input and then using that input to return an output with the unique input.

Let's start by heading back into our hello.rb file and we'll change our output from saying "Hello World!" to a question asking our name. Modify line three to the following:

puts "What is your name?"

We are now using the puts method to output a question asking us our name. Below that we have our second lesson which will allow us to enter our name and capture our input. Uncomment the code on line seven so that it looks like this:

name = gets

The seventh line introduces a new method, gets, and a variable called name.

What the gets method does is tell the computer to pause for input from the user. Our program will pause to allow us to enter in any text we want, and when we're ready to resume the program, we can press the ENTER key. Our inputs are then captured and converted to a string.

Variables are a way to store information. Similar to how methods are used to store code that can later be used by calling the method, variables store information like strings that can later be accessed by calling the variable. For example, if I assign the string Octocat to the variable name, I could then call puts name and the output would be the string Octocat.

We put this to use in our eighth line of code. Uncomment the eighth line and your project should have the following code:

puts "Hey, #{name}! Nice to meet you!"

This new line introduces another new Ruby feature called string interpolation. String interpolation is used to take the content within a variable and insert it into a string. For example, the puts method above will output the value within the variable name rather than the word name itself.

We can see this in action by saving our file and running our Ruby file in the command line again. Our program should now ask us our name. We will then be able to enter our name and after entering our name and pressing the ENTER key, we will see a new output with our name in that output.

It may not look how we expect because of an unexpected line break after our name. That's because the gets method captures all the keystrokes including the ENTER key. The ENTER key inside a string creates a newline character, and that newline character is what creates the line break. There is a way we can fix this though by using a method called chop on the captured string.

The chop method takes a string and removes the very last character of that string, in this case, the newline character.

Let's head back into our hello.rb file and add the chop method after our gets method. Modify the seventh line so that it looks like this:

name = gets.chop

Save the file and now if we run our program in the terminal we should see that the line break is no longer. Our output now looks as we expect it to and we've successfully created a Ruby program that outputs strings, prompts us for input, and outputs new strings using our inputs.

Your hello.rb file should look like this:

# Lesson 1: Outputting a String

puts "What is your name?"

# Lesson 2: Prompting User Input

name = gets.chop
puts "Hey, #{name}! Nice to meet you!"

Before moving forward to the next section, let's commit and push our changes to the repo following the steps outlined above.

git add .
git commit -m"add ruby methods"
git push

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.