find the first character in a string in ruby

You can find the first character in a string in Ruby by using indexing with [0] or .chr method. Here's an example using indexing:

main.rb
str = "hello"
first_char = str[0]
puts first_char    # Output: "h"
67 chars
4 lines

Alternatively, you can use the .chr method to get the first character:

main.rb
str = "hello"
first_char = str.chr
puts first_char    # Output: "h"
68 chars
4 lines

related categories

gistlibby LogSnag