This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Our first requirement is to print a welcome message for the user. To do that, we'll need to learn how to call "methods".
Ruby source files
- Ruby source code is stored in plain text files, but instead of
.txt
, the file usually ends in.rb
. - Run
ruby widgets.rb
.- To run a Ruby program, we need to run the
ruby
command in the terminal or console. - Type
ruby
, a space, and the name of the file you want to run. As long as you're in the same folder or directory as that file, then all you need to type is the file name.
- To run a Ruby program, we need to run the
If you have a script named other.rb
that's in a folder named my_folder
, you have two ways to run it:
-
ruby my_folder/other.rb
OR -
cd my_folder
, thenruby other.rb
Methods
puts "hello world"
-
puts
is a method, a group of code statements that together perform a particular task.-
puts
stands for "put string", and that's the task it carries out: it puts a string of text characters on the terminal output. - Typing the method name in your code calls that method, or causes it to run.
- The text inside quotes is a string. We'll be talking more about strings soon.
- Because we're putting the string here following the method call, it's passed as an argument to the method.
-
puts(1, 2, "a", "b")
sleep(2)
print(1, 2, "a", "b")
sleep(1)
- Parentheses are optional
puts 1, 2, "a", "b"
sleep 2
print 1, 2, "a", "b"
sleep 1
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up