hello,
how could I chunk_split string
mylongstring = “some long long long string”
into smaller strings of length 3.
In PHP it is
chunk_split(mylongstring,3);
What is it in ruby?
Thank you in advance!
hello,
how could I chunk_split string
mylongstring = “some long long long string”
into smaller strings of length 3.
In PHP it is
chunk_split(mylongstring,3);
What is it in ruby?
Thank you in advance!
Mister T. wrote:
irb(main):005:0> “some long long long string”.scan(/.{1,3}/)
=> [“som”, “e l”, “ong”, " lo", "ng ", “lon”, “g s”, “tri”, “ng”]
Like that? If not, you’ll need to give an example of the output you’re
trying to achieve (which is generally a good idea anyway…).
On Apr 27, 4:44 am, Mister T. [email protected] wrote:
What is it in ruby?
Thank you in advance!
–
Posted viahttp://www.ruby-forum.com/.
long_string = “some long long long string”
p long_string.scan(/.{3}/)
On Behalf Of Firstname Secondname:
Ah, do not forget, in ruby land, you can be flexible
C:\family\ruby>cat test.rb
class String
alias old_split split
def split arg1=’ ', *args
case arg1
when Integer
self.scan(/.{1,#{arg1}}/)
else
old_split arg1, *args
end #case
end #def
end #class
p “this is a test string. okay?”.split
p “this is a test string. okay?”.split(/i/)
p “this is a test string. okay?”.split(3)
C:\family\ruby>ruby test.rb
[“this”, “is”, “a”, “test”, “string.”, “okay?”]
[“th”, "s “, “s a test str”, “ng. okay?”]
[“thi”, “s i”, “s a”, " te”, "st ", “str”, “ing”, “. o”, “kay”, “?”]
Is that ok?
kind regards -botp
Alex Y. wrote:
Mister T. wrote:
irb(main):005:0> “some long long long string”.scan(/.{1,3}/)
=> [“som”, “e l”, “ong”, " lo", "ng ", “lon”, “g s”, “tri”, “ng”]Like that? If not, you’ll need to give an example of the output you’re
trying to achieve (which is generally a good idea anyway…).
Exactly like that
Thank you
class String
alias old_split split
cool
Peña, Botp wrote:
On Behalf Of Firstname Secondname:
Exactly like that
Ah, do not forget, in ruby land, you can be flexible
C:\family\ruby>cat test.rb
class String
alias old_split split
def split arg1=’ ', *args
case arg1
when Integer
self.scan(/.{1,#{arg1}}/)
else
old_split arg1, *args
end #case
end #def
end #classp “this is a test string. okay?”.split
p “this is a test string. okay?”.split(/i/)
p “this is a test string. okay?”.split(3)C:\family\ruby>ruby test.rb
[“this”, “is”, “a”, “test”, “string.”, “okay?”]
[“th”, "s “, “s a test str”, “ng. okay?”]
[“thi”, “s i”, “s a”, " te”, "st ", “str”, “ing”, “. o”, “kay”, “?”]Is that ok?
kind regards -botp
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs