|
You can use "print". For e.g., print "Hello World"
|
You can use "puts". For e.g., puts "Hello World"
|
|
When you use print, everything gets printed out on the same line
|
When you use puts, everything gets printed out on a new line
|
Let's say we want to print out the message:
I am 18 years old. I like ice skating. I went ice skating for the first time when I was 18.
You can create variables:
character_age = "18"
character_sport = "ice skating"
To print this message, write:
character_age = "18"
character_sport = "ice skating"
puts ("I am " + character_age + " years old.")
puts ("I like " + character_sport)
puts ("I went " + character_sport + " for the first time when I was " + character_age)
You can also modify the message half way:
character_age = "18"
character_sport = "ice skating"
puts ("I am " + character_age + " years old.")
puts ("I like " + character_sport)
character_sport = "cycling"
puts ("I went " + character_sport + " for the first time when I was " + character_age)
It will output:
I am 18 years old. I like ice skating. I went cycling for the first time when I was 18.
String/Text
name = "Mike"
Integer (A full number)
age = 18. Note that you do not need to include quotations.
Floating Point (A number that has a decimal)
number = 18.9
Boolean (on/off, true/false, 1/0)
isfemale = true
Nil (referring to "nothing". Absence of a value)
flaws = nil
puts "Enter your name: "
name = gets.chomp()
puts "Enter your age: "
age = gets.chomp()
puts ("Hello " + name + ", you are" + age + " years old")
friends = Array["Kevin", "Karen", "Oscar"]
puts friends[-2]
It will output: Karen
If you want to select names from index 0 - 2:
friends = Array["Kevin", "Karen", "Oscar"]
puts friends[0, 2]
It will output: Kevin, Karen. Note that is does
not include Oscar.
def write_data_to_file(file_name)
a_file.puts('5')
a_file.puts('Fred')
a_file.puts('Sam')
a_file.puts('Jill')
a_file.puts('Jenny')
a_file.puts('Zorro')
a_file.close()
end
|
puts "Giraffe\" Academy"
|
|
It will output: Giraffe" Academy
|
print "Hello\n World"
It will output:
Hello
World
puts "Enter your name"
name = gets
puts ("Hello " + name + " , you are cool!")
"gets" alone will put ", you are cool!" on a new line.
To mitigate this, we use gets.chomp to ensure that
"Hello " + name + " , you are cool!" is on one line.
require 'date'
def hello()
puts("what year were you born?")
year_born = gets.chomp()
age = Date.today.year - year_born.to_i() - 1
puts("So you are " + age.to_s + " years old")
end
def
main()
hello()
end
main()
phrase = "Giraffe Academy"
puts phrase.upcase()
puts phrase.downcase()
It will output:
GIRAFFE ACADEMY
giraffe academy
phrase = " Giraffe Academy "
puts phrase.strip()
It will output "Giraffe Academy"
phrase = "Giraffe Academy"
puts phrase.length()
It will output: 15
phrase = "Giraffe Academy"
puts phrase[0,3]
It will output: Gir
phrase = "Giraffe Academy"
puts phrase.index("ffe")
It will output the index position it starts at: 4
class Book
attr_accessor :title, :author, :pages
end
book1 = Book.new()
book1.title = "Harry Potter"
book1.author = "JK Rowling"
book1.pages = 400
puts book1
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.