Store values in variables
Name data and work with numbers, strings, and booleans.
- 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.
1language = "Ruby"
2year = 1995
3ready = true
4puts "#{language} began in #{year}: #{ready}"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.
Double a number
Read an integer from standard input, convert it with to_i, and print twice its value.
- Input 4
- Input -3
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…