Ruby - String
Ruby - String
# string interpolation
puts "Enter name of an animal"
animal = gets.chomp
puts "Enter a noun"
noun = gets.chomp
p "the quick brown #{animal} jumps over the lazy #{noun}"
# try again with single quote
p 'the quick brown #{animal} jumps over the lazy #{noun}'
# Other example
p "the quick brown #{2 + 2}"
2.7.1 :003 > "Ruby" << " programming" << " language"
=> "Ruby programming language"
2.7.1 :091 > s = "the quick brown fox jumps over the lazy dog"
=> "the quick brown fox jumps over the lazy dog"
2.7.1 :092 > s.split
=> ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
2.7.1 :093 > s.split(' ')
=> ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
2.7.1 :094 > s.split(/ /)
=> ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
2.7.1 :095 > s1 = " the quick brown fox jumps over the lazy dog "
=> " the quick brown fox jumps over the lazy dog "
2.7.1 :096 > s1.split(/ /)
=> ["", "the", "", "", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
2.7.1 :127 > s = "\t \r the quick brown fox jumps over the lazy dog "
=> "\t \r the quick brown fox jumps over the lazy dog "
2.7.1 :128 > s.strip
=> "the quick brown fox jumps over the lazy dog"
2.7.1 :129 > s1 = "\t \r the quick brown fox jumps over the lazy dog \n "
=> "\t \r the quick brown fox jumps over the lazy dog "
2.7.1 :130 > s1.strip
=> "the quick brown fox jumps over the lazy dog"