Practice
What is the result of each of the following?
-
puts "What is the return result" + " of " + "this operation?"
-
"This string minus" - "That string"
-
"1234.55".to_i -
"1234.55".to_f -
"Not a number".to_i -
puts "1\n2\n3\n" -
Why might it be useful that the to_i (to integer) method return zero for strings that can’t be represented as numbers?
-
What do you think the length method does?
"Count".length -
How about the split method?
"Count".split("") -
What do you think slice does?
"Count".slice(2)
Want to see more cool String methods? Just call methods on the String class! Type String.methods into your IRB:
IRB$ String.methods
# some of the built-in Ruby methods for String
[:try_convert, :allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_
and many more...