Git Product home page Git Product logo

booleans-ruby-readme's Introduction

Booleans and Comparisons

Objectives

  1. Review the concept of boolean values.
  2. Use boolean operators and identify their return values.
  3. Use comparison operators and identify their return values.

Booleans

We've already learned a bit about the boolean (true-or-false) data type. In Ruby, a boolean refers to a value of either true or false, both of which are defined as their very own data types. Every appearance, or instance, of true in a Ruby program is an instance of TrueClass, while every appearance of false is an instance of FalseClass.

For now, we don't need to understand the concept of classes in depth. Just know that classes serve as templates for Ruby objects. Think of TrueClass and FalseClass like cookie cutters––there is a TrueClass cookie cutter and a FalseClass cookie cutter and every appearance of true or false is like a cookie made with its respective cookie cutter.

Boolean Operators

How do we create boolean values in a Ruby program? Well, you can actually type true or false or we can write statements that return true or false. Now that we understand the concept of "truthiness"—that certain types of data are "truthy" and certain others are "falsey"—we can understand how to write such statements.

We already know that Strings are one type of data that are truthy. Drop into IRB and use !! (the "double-bang operator") to determine that the string "hi" is truthy:

!!"hi" #=> true

Note: You may see a warning regarding String literals when you run the above code. Ignore it! Ruby is just reminding us that it is a little strange to use the literal string "hi" in a statement, instead of saving it to a variable first. For example:

string = "hi"
!!string #=> true

We used the literal string in this example though to highlight the "truthy" nature of strings.

In the next unit, we will learn how to use the native truthiness of data types like strings to write statements that return true.

First, we're going to learn how to use boolean operators to write statements that return true or false.

What are Boolean Operators?

Boolean operators are really methods which means that they have return values. What do they return? true or false of course!

In Ruby there are three main boolean operators:

  • ! ("single-bang") which represents "NOT",
  • && ("double-ampersand") which represents "AND", and
  • || ("double-pipe") which represents "OR".

For an && ("and") to evaluate to true, both values of either side of the symbol must evaluate to true. For example:

true && true #=> true

true && false #=> false

For an || ("or") to evaluate to true, only one value on either side of the symbol must evaluate to true. For example:

false || true #=> true

Finally, a ! ("not") reverses the logical state of its operand: if a condition is true, then ! will make it false; if it is false, then ! will make it true. For example:

!true #=> false

!false #=> true

Comparison Operators

To check if two values are equal, we use the comparison operator represented with == ("double-equal-sign"). If two values are equal, then the statement will return true. If they are not equal, then it will return false. For example:

1 == 1 #=> true

1 == 7 #=> false

Top-tip: The comparison operator == is distinct from the assignment operator = that is used to set a variable equal to a value. Mistaking these for each other is a common cause of unexpected behavior.

More Comparison Operators

Ruby is good at comparing things. For instance, it knows that 14 is larger than 3. Let's see that in action.

14 > 3 #=> true

Here, 14 is larger than 3, so Ruby evaluates this to true. Comparisons in Ruby always evaluate to true or false.

The commonly used comparison operators are:

Operator Operation
== If the values of the two operands are equal, then the evaluation is true.
!= If the values of the two operands are not equal, then the evaluation is true.
> If the value of the left operand is greater than the value of the right operand, then the evaluation is true.
< If the value of the left operand is less than the value of the right operand, then the evaluation is true.
>= If the value of the left operand is greater than or equal to the value of the right operand, then the evaluation is true.
<= If the value of the left operand is less than or equal to the value of the right operand, then the evaluation is true.

Ruby can compare a lot more than just numbers. It can also compare strings:

"yellow" == "yellow" #=>true

And variables with known values:

my_mood = "happy"

my_mood == "happy" #=> true

It can also compare variables against other variables:

easter_eggs = 16
ducklings = 3

easter_eggs > ducklings #=> true

ducklings >= easter_eggs #=> false

ducklings == easter_eggs #=> false

# if you call class on a variable, you can see if it's a string, an integer, etc.

ducklings.class #=> Fixnum
easter_eggs.class #=> Fixnum
ducklings.class == easter_eggs.class #=> true

Comparison operators are essential to developing logical flow.

booleans-ruby-readme's People

Contributors

annjohn avatar aviflombaum avatar deniznida avatar dougtebay avatar fislabstest avatar fs-lms-test-bot avatar ihollander avatar joshuabamboo avatar kthffmn avatar lizbur10 avatar lwadya avatar markedwardmurray avatar maxwellbenton avatar msuzoagu avatar sarogers avatar sophiedebenedetto avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

booleans-ruby-readme's Issues

Readme clean up

remove tags table, add to .learn file, add objectives

maybe add quiz after?

I would like to suggest putting the quiz at the end of the lesson.

I think the quiz ought to be at the end of the lesson because the lesson currently has the quiz before the "More Comparison Operators" section. The problem with this is that the quiz uses !=, which is not introduced until the "More Comparison Operators" section.

Another alternative to switching the quiz and the "More Comparison Operators" section around would simply be to introduce != in the section before the quiz.

I realize that this is minor, but it could potentially avoid confusion from students who have not encountered != before. Thanks for looking into this!

Sdcrouse

Error in Operator Operations

Under Operator Operations it says >= represents greater than or equal to. I believe its supposed to say >= instead.

Quiz Issue

My quiz will not mark as complete.

I think a couple of the questions are buggy because they did not have any "true" or "false" options.

Re-add Quiz

We had:

???

# Logical Operators Quiz

?: `true && true`

(X)`true` ( )`false`

?: `false && true`

( )`true` (X)`false`

?: `1 == 1 && 2 == 1`

( )`true` (X)`false`

?: `"test" == "test"`

(X)`true` ( )`false`

?: `1 == 1 || 2 != 1`

(X)`true` ( )`false`

?: `true && 1 == 1`

(X)`true` ( )`false`

?: `false && 0 != 0`

( )`true` (X)`false`

?: `true || 1 == 1`

(X)`true` ( )`false`

?: `"test" == "testing"`

( )`true` (X)`false`

?: `1 != 0 && 2 == 1`

( )`true` (X)`false`

?: `"test" != "testing"`

(X)`true` ( )`false`

?: `"test" == 1`

( )`true` (X)`false`

?: `!(true && false)`

(X)`true` ( )`false`

?: `!(1 == 1 && 0 != 1)`

( )`true` (X)`false`

?: `!(10 == 1 || 1000 == 1000)`

( )`true` (X)`false`

?: `!(1 != 10 || 3 == 4)`

( )`true` (X)`false`

?: `!("testing" == "testing" && "Zed" == "Cool Guy")`

(X)`true` ( )`false`

?: `1 == 1 && (!("testing" == 1 || 1 == 0))`

(X)`true` ( )`false`

?: `"chunky" == "bacon" && (!(3 == 4 || 3 == 3))`

( )`true` (X)`false`

?: `3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun"))`

( )`true` (X)`false`


???

I removed it because it's from Zed's work. I updated the examples - we could use those - but let's just make a new quiz.

@SophieDeBenedetto @AnnJohn

Practice examples

Weird to mark all the correct answers for each of these practice examples, isn't it?

better explanation

we need to briefly explain what we mean by boolean (again), how operators work to return true/false in beginning.

wrong answer

number and conditions has the wrong answer on problem # 15
it should be true.

Clarification of istrue and isfalse for Quiz

The use of istrue and isfalse as questions and answers for the quiz can be confusing. Since they are not defined variables, if read literally, the question seems to ask for the return value of the boolean operator with two undefined values.

Example of a question and two answer choices:

isfalse && istrue
istrue
is false

To avoid confusion, a short note at the beginning of the quiz would help. For example, immediately after "Let's Practice!":
Note: for the quiz, istrue represents the boolean true or any statement that evaluates to true (a truthy statement), and isfalse represents the boolean false or any statement that evaluates to false (a falsey statement).

Suggest pry > irb

In the boolean operators section of this module, we suggest they:

Drop into IRB and use !! (the "double-bang operator") to determine that the string "hi" is truthy:

Would it be better to suggest they "drop into pry" instead of IRB, since we already had students download and use it in the debugging with pry module?

Quiz not displaying properly

There are 2 or 3 questions on the quiz that don't give an option to answer them. Since I can't enter any input, I can't complete the quiz or the lesson.

I found a bug when navigating to this lesson!

First of all, this problem seems to be limited only to this specific lesson, but I need to test this out further to be sure.

The problem is that whenever I navigate to this specific lesson, somehow the title of the page does not read "Boolean Operators" like it's supposed to. Instead, it takes the title of the lesson that I was previously on. Not only that, but it has the navigation bar (Sandbox/Open IDE, Open in GitHub, and Raise an Issue) from the previous lesson as well.

This is true regardless of how I navigate: by hitting "Next Lesson" from "Booleans and Truthiness in Ruby" (the lesson preceding the Boolean Operators lesson), by hitting the "back" button to go from my current lesson back into "Boolean Operators", and even by using the Curriculum navigation bar to go to "Boolean Operators" when I'm in another lesson.

I tried fixing this by navigating to the "Boolean Operators" lesson when I'm in that lesson, but it will not work. The reason for this is that the navigation bar is ALSO showing that I am still in the other lesson. The only way that I can currently fix this is by hitting "Refresh".

Here's an example of what I'm talking about:
booleanoperatorsbug1

Notice above that the lesson content, tab title, and web address are all correct, but the title on the page itself (HTML Basics) is incorrect. And as shown below, the Curriculum navigation menu also shows that I am currently in "HTML Basics" rather than "Boolean Operators":

booleanoperatorsbug2

I realize that this isn't the biggest bug out there, but it may not be limited to this lesson only. Thanks for checking this out!

Sdcrouse

IRB returns a warning when using !!"string" notation

The page says to type !!"hi" to see a true response, but irb warns when this happens; might want to give users a heads-up and explain why it returns such an error.

2.2.2 :004 > !!"hi"
(irb):4: warning: string literal in condition
=> true
2.2.2 :005 >

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.