Um momento
0x10Lesson 2 of 6

Store values in variables

Name data and work with numbers, strings, and booleans.

13 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Create and use Ruby local variables.

Assign a value to a local variable with =. Ruby determines the object’s class from the value, so you do not write a type before the variable name. Names beginning with a lowercase letter or underscore are local variables.

main.rb
1language = "Ruby"
2year = 1995
3ready = true
4puts "#{language} began in #{year}: #{ready}"
Output
Ruby began in 1995: true

Ruby values are objects. Strings hold text, integers hold whole numbers, and true and false are boolean values. #{...} inside a double-quoted string evaluates Ruby code and inserts its result.

Key takeaways

  • Create and use Ruby local variables.

  • Ruby methods and blocks help express operations clearly.

  • Read each result and error message as feedback about your code.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Ruby

Write Ruby in the editor and run it against sample input. Your code is sent to Wandbox to run.

Exercise 1

Double a number

+25 XP

Read an integer from standard input, convert it with to_i, and print twice its value.

  • Input 4
  • Input -3
main.rb
Loading editor…

Ruby runs through Wandbox, a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: