[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

simple question, looping through each character in a string

warhero

11/30/2006 6:15:00 PM

how can I accomplish something like this in ruby:

pseudo code:

word = "picture"
for( i = 0; i < word.length; i++ )
{
puts( word.substr(i,1) )
}


ruby?

I've tried something like:

word = "picture"
word.each { |char| puts char }

but that doesn't do what I am wanting. it ends up just putting the
entire word "picture"

I've also tried:

word = "picture"
for i in 0..word.length - 1
puts word[i]
end

that just puts out ascii numbers..

I thought strings could be access like arrays?
word = "picture"
puts word[0] -> 116


thanks all

-rubynube

17 Answers

dblack

11/30/2006 7:13:00 PM

0

warhero

11/30/2006 7:17:00 PM

0

i've never seen a split('//'). what exactly is that doing?

thanks



On Nov 30, 2:13 pm, dbl...@wobblini.net wrote:
> Hi --
>
> On Fri, 1 Dec 2006, warhero wrote:
> > how can I accomplish something like this in ruby:
>
> > pseudo code:
>
> > word = "picture"
> > for( i = 0; i < word.length; i++ )
> > {
> > puts( word.substr(i,1) )
> > }There's an each_byte iterator. It gives you ASCII values, so you have
> to convert them:
>
> word = "picture"
> word.each_byte {|b| puts b.chr }
>
> You can also do:
>
> word.split(//).each {|char| puts char }
>
> (Note that this is an area of Ruby that's undergoing a lot of changes
> in the transition from 1.8 to 1.9/2.0.)
>
> David
>
> --
> David A. Black | dbl...@wobblini.net
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
> [1]http://www.manning...| [3]http://www.rubypoweran...
> [2]http://dablog.r... | [4]http://www.rubyc...

Drew Olson

11/30/2006 7:24:00 PM

0

Aaron Smith wrote:
> i've never seen a split('//'). what exactly is that doing?
>
> thanks

split will return an array of strings, splitting the initial string at
any point that matches the supplied regex.

--
Posted via http://www.ruby-....

James Gray

11/30/2006 7:29:00 PM

0

On Nov 30, 2006, at 1:13 PM, dblack@wobblini.net wrote:

> You can also do:
>
> word.split(//).each {|char| puts char }

Or without the Array:

word.scan(/./m) { |char| ... }

Or you can load the each_char() method from the standard library:

require "jcode"
word.each_char { |char| ... }

James Edward Gray II

dblack

11/30/2006 7:39:00 PM

0

warhero

11/30/2006 7:40:00 PM

0

Yes, I saw the each_char method in the RDocs but when I tried using it,
it gave me an error. Now I see I had to load it. i'll try that.

thanks.

On Nov 30, 2:29 pm, James Edward Gray II <j...@grayproductions.net>
wrote:
> On Nov 30, 2006, at 1:13 PM, dbl...@wobblini.net wrote:
>
> > You can also do:
>
> > word.split(//).each {|char| puts char }Or without the Array:
>
> word.scan(/./m) { |char| ... }
>
> Or you can load the each_char() method from the standard library:
>
> require "jcode"
> word.each_char { |char| ... }
>
> James Edward Gray II

warhero

11/30/2006 7:47:00 PM

0

does it make sense to have a method for String in a file called
jcode.rb? I'm just getting into ruby, I haven't had to do a lot yet
where I needed t require some other libraries. is all of ruby have
weird names for where code is?




On Nov 30, 2:29 pm, James Edward Gray II <j...@grayproductions.net>
wrote:
> On Nov 30, 2006, at 1:13 PM, dbl...@wobblini.net wrote:
>
> > You can also do:
>
> > word.split(//).each {|char| puts char }Or without the Array:
>
> word.scan(/./m) { |char| ... }
>
> Or you can load the each_char() method from the standard library:
>
> require "jcode"
> word.each_char { |char| ... }
>
> James Edward Gray II

matt

11/30/2006 8:09:00 PM

0

warhero <beingthexemplarylists@gmail.com> wrote:

> how can I accomplish something like this in ruby:
>
> pseudo code:
>
> word = "picture"
> for( i = 0; i < word.length; i++ )
> {
> puts( word.substr(i,1) )
> }
>
>
> ruby?
>
> I've tried something like:
>
> word = "picture"
> word.each { |char| puts char }
>
> but that doesn't do what I am wanting. it ends up just putting the
> entire word "picture"
>
> I've also tried:
>
> word = "picture"
> for i in 0..word.length - 1
> puts word[i]
> end
>
> that just puts out ascii numbers..
>
> I thought strings could be access like arrays?
> word = "picture"
> puts word[0] -> 116

What I do is this:

the_string.scan(/./).each do |char|

However, do note that, as others have said, in Ruby 1.9 this will no
longer be necessary (though it will still work). m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

warhero

11/30/2006 8:55:00 PM

0

does anyone know how you will accomplish the same thing in ruby 1.9?



On Nov 30, 3:09 pm, m...@tidbits.com (matt neuburg) wrote:
> warhero <beingthexemplaryli...@gmail.com> wrote:
> > how can I accomplish something like this in ruby:
>
> > pseudo code:
>
> > word = "picture"
> > for( i = 0; i < word.length; i++ )
> > {
> > puts( word.substr(i,1) )
> > }
>
> > ruby?
>
> > I've tried something like:
>
> > word = "picture"
> > word.each { |char| puts char }
>
> > but that doesn't do what I am wanting. it ends up just putting the
> > entire word "picture"
>
> > I've also tried:
>
> > word = "picture"
> > for i in 0..word.length - 1
> > puts word[i]
> > end
>
> > that just puts out ascii numbers..
>
> > I thought strings could be access like arrays?
> > word = "picture"
> > puts word[0] -> 116What I do is this:
>
> the_string.scan(/./).each do |char|
>
> However, do note that, as others have said, in Ruby 1.9 this will no
> longer be necessary (though it will still work). m.
>
> --
> matt neuburg, phd = m...@tidbits.com,http://www.tidbits...
> Tiger -http://www.takecontrolbooks.com/tiger-custom...
> AppleScript -http://www.amazon.com/gp/product/...
> Read TidBITS! It's free and smart.http://www.t...

Trans

11/30/2006 9:03:00 PM

0



warhero wrote:
> does anyone know how you will accomplish the same thing in ruby 1.9?

str.chars.each |c| ...

T.