Onur Özgür ÖZKAN

Php, Ruby, Kebab, Git Geek

Text and String at Ruby

First of all, remember that everything are object at ruby.

1
2
3
4
puts "Hello, world!".class

# Output
# String

You can add and compare the string

1
2
3
4
5
6
a = 'First'
b = 'Second'
puts "Success!" if a + b == "FirstSecond"

# Output
# Success!

Using quotation mark is only viable for a single line, but if you want to span multiple lines, you can do like that

1
2
text = %q{This text is
multi line capabilities}

You can use any delimiters of your choice.

1
2
text = %q<This text is
multi line capabilities>

Here document is

1
2
3
4
text = <<END_MY_STRING_PLEASE
This is the string
And a second line
END_MY_STRING_PLEASE

Multiply strings

1
2
3
4
puts "dudu1" * 5

# Output
# dudu1dudu1dudu1dudu1dudu1

Greater than or less than

1
2
3
4
5
6
7
8
9
10
puts "x" > "y"

# output
# false


puts "x" < "y"

# output
# true

Note : Every letter or symbol has a value, called an ASCII value. You can see them with ? operator.

1
2
3
4
5
6
7
8
9
puts ?x

# output
# 120

puts ?A

# output
# 65

You can achieve the inverse by using the String Class’s chr method.

1
2
3
4
5
6
puts ?x
puts 120.chr

# output
# 120
# x

Interpolation in Ruby

1
2
3
4
5
6
x = 10
y = 20
puts "#{x} + #{y} = #{x + y}"

# output
# 10 + 20 = 30

Best Regards.