Ruby Strings
# Ruby Basics
#Strings
#Get input from a user and assign it to a variable:
name = gets
#Put the contents of a name in another string via interpolation:
puts "Hello #{name}!"
#Assuming the variable "name" contained "Treehouse", that would print "Hello Treehouse!".
#Print out the characters without string interpolation:
puts 'Hello #{name}!'
#That would print out the text "Hello #{name}!" regardless of what is contained in the name variable.
#Methods of creating interpolated strings:
#Double quotes:
string = "Hello"
#With %Q: do interpolation - it prints the name
string = %Q(Hello World!)
#Multiple lines:
string = "
My long string
On multiple lines
Containes newline characters."
#Heredoc:
string = <<-EOF
Anything placed between
The characters at the top of the string
Wil go inside it.
It is ended with the same characters.
EOF
name = "Jason"
string = <--HELLO
My long #{name}
On multiple lines
Containes newline characters.
HELLO
#it print the name
puts string
#Methods of crating strings without interpolation:
#Single quotes:
string = 'Hello World!'
#%q:
string = %q(Hello world!)
--------------
#Special characters:
#\n newline \t tab \s space
#Use a newline in a String:
puts "Hello\nWorld"
#Prints:
Hello
World
#Use a tab in a String:
puts "Hello\tWorld"
#Prints:
Hello World
--------------
#String Methods
#Create a string variable with your name:
name = "Jason"
#Uppercase a string:
name.upcase
#Turn a string in to lowercase:
name.downcase
#Reverse a string:
name.reverse
#All together
name.reverse.upcase.downcase.reverse
#assign to a variable
uppercase_name = name.upcase
uppercase_name
# print name in uppercase
--------------
#String Documentation
#Concepts
#Methods that are named with an exclamation point at the end generally will change the variable value rather than return a new variable.
#Reading the documentation allows you to look up new methods you may not have seen before and reveals how methods work as well as what they return.
--
upcase -> new_str
#new_str : Return a new string but it doesn't change it completelyyyyy
---
upcase! -> str or nil (upcase with an exclamation)
#the variable was change - it change the variable
a_string = "This is a String"
another_string = 'This is also a String'
# Multi-line string """
beginning = """
Mon
Tue
Wed
Thur
Friday
Saturday
Sunday
"""
# ---------------
#String interpolation
#Another advantage to using double quoted strings is
#that it allows us to interpolate values inside strings:
name = "Nizar"
puts "Hi #{name}" (Put value inside inside like a template)
minute = 60
hour = 60 * minute
day = 24 * hour
puts "Seconds in a #{day}"
puts day
# Working with strings
first_name = priss
last_name = rodriguez
first_name + " " + lastname
# concatinate and added to the first_name variable
first_name << " " << lastname
# http://ascii-code.com
#ASCII table Code
first_name[0]
112 #p minuscula
name = gets
# type name
prissrodriguez
# ------
string = "hello world\n"
"hello world\n"
# inspect method show variable
puts string.inspect
"hello world\n"
# chomp method no white spaces at end
puts string.chomp
hello world
# chomp method no white spaces
string.chop
"hello world"
# include method ask if the world is on the string
string.include?("world")
true
string.include?("priss")
false
# replace method one string with another
string.replace("priss")
# index method what position in the string de letter ocurr
string.index("i")
2
# reverse method reverse all letter in string
string.reverse
ssirp
# upcase method
string.upcase
PRISS
# downcase method
string.downcase
priss
# swapcase method change upcase to down and down to upper
string.swapcase
pRISS
# length method length of string including spaces
string.length
5
# size method the same as length
string.size
5
# split method return the string in an array
string.split(" ")
["Hello", "world"]
string.concat("Another string")
"Hello world Another string"
# Bang methods end with an exclamation point ! and are considered DANGEROUS
# Modify the receiver in place
string.reverse!
ssirp