7

I know this question is a fundamental one. I am able to take user input for a string and for an integer using:

str = gets()
num = gets().to_i

But I want to read from the String(say which is in my case more than a line in length) character by character and count the number of characters from the first to the very last for every character encountered in the string. I know this can be achieved through:

str.length

I want to count it character wise as I am trying to implement word wrap in Ruby, wherein say within the line width(which would be a user defined number input) I would want to print only those words which are not continuing over to the next line i.e. I don't want to split a continuous word over two lines. Such words should be taken to the new line.

Thanks for you time..!!

boddhisattva
  • 6,908
  • 11
  • 48
  • 72

2 Answers2

7

getc will read in a character at a time:

char = getc()

Or you can cycle through the characters in the string with each_char:

'abc'.each_char do |char|
  puts char
end  
bowsersenior
  • 12,524
  • 2
  • 46
  • 52
  • 1
    This answers what was originally asked in the title of the question, but not what was actually asked for. – Phrogz Dec 23 '10 at 01:33
  • LOL! I guess answering the title was good enough for the OP. Glad my answer could be of some help. – bowsersenior Dec 24 '10 at 03:08
  • I guess my question was mainly to do with the "how to" input a character in ruby so I am just giving justice to that. I am also grateful to those who have implemented word wrap for me through regular expressions in Ruby( from their answers and I have I hope by giving a +1 at least done some justice to their effort in making things simpler for me).. but without any offence..... I was not looking for the solution on how to implement word wrap.. – boddhisattva Dec 24 '10 at 16:30
  • @mgj From your question I thought you would find my answer useful. It's also very interesting to see the very different answers from others. I like Stack Overflow because answers and questions are from different perspectives, so you get info that is more interesting and diverse than from an "official" API or manual. – bowsersenior Dec 24 '10 at 18:05
5

You might want to check out the Text::Format gem. Also, Rails has word_wrap as part of ActionView, and Padrino has a similar word_wrap method if you're doing web stuff.

Otherwise this is a linewrap sample from: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/249306

str = "\
I don't necessarily need code examples -- but if anyone has
ideas for a best approach to specifying a line wrap width
(breaking between words for lines no longer than a specific
column width) for output from a Ruby script, I'd love to
hear about it."

X = 40
puts str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)


--- output ---
I don't necessarily need code examples
-- but if anyone has ideas for a best
approach to specifying a line wrap width
(breaking between words for lines no
longer than a specific column width) for
output from a Ruby script, I'd love to
hear about it.
the Tin Man
  • 158,662
  • 42
  • 215
  • 303