Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > php's chunk_split alternative in ruby

Reply
Thread Tools

php's chunk_split alternative in ruby

 
 
Mister Twister
Guest
Posts: n/a
 
      04-27-2007
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!

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

 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      04-27-2007
On Apr 27, 4:44 am, Mister Twister <(E-Mail Removed)> wrote:
> 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!
>
> --
> Posted viahttp://www.ruby-forum.com/.


long_string = "some long long long string"
p long_string.scan(/.{3}/)

 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      04-27-2007
Mister Twister wrote:
> 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);
>

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...).

--
Alex


 
Reply With Quote
 
Firstname Secondname
Guest
Posts: n/a
 
      04-27-2007
Alex Young wrote:
> Mister Twister 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

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

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      04-27-2007
T24gQmVoYWxmIE9mIEZpcnN0bmFtZSBTZWNvbmRuYW1lOg0KIy BFeGFjdGx5IGxpa2UgdGhhdCA6
KQ0KDQpBaCwgZG8gbm90IGZvcmdldCwgaW4gcnVieSBsYW5kLC B5b3UgY2FuIGJlIGZsZXhpYmxl
IDopDQoNCkM6XGZhbWlseVxydWJ5PmNhdCAgdGVzdC5yYg0KY2 xhc3MgU3RyaW5nDQogICBhbGlh
cyBvbGRfc3BsaXQgc3BsaXQNCiAgIGRlZiBzcGxpdCBhcmcxPS cgJywgKmFyZ3MNCiAgICAgIGNh
c2UgYXJnMQ0KICAgICAgICAgd2hlbiBJbnRlZ2VyDQogICAgIC AgICAgICBzZWxmLnNjYW4oLy57
MSwje2FyZzF9fS8pDQogICAgICAgICBlbHNlDQogICAgICAgIC AgICBvbGRfc3BsaXQgYXJnMSwg
KmFyZ3MNCiAgICAgIGVuZCAjY2FzZQ0KICAgZW5kICNkZWYNCm VuZCAjY2xhc3MNCg0KcCAidGhp
cyBpcyBhIHRlc3Qgc3RyaW5nLiBva2F5PyIuc3BsaXQNCnAgIn RoaXMgaXMgYSB0ZXN0IHN0cmlu
Zy4gb2theT8iLnNwbGl0KC9pLykNCnAgInRoaXMgaXMgYSB0ZX N0IHN0cmluZy4gb2theT8iLnNw
bGl0KDMpDQoNCkM6XGZhbWlseVxydWJ5PnJ1YnkgdGVzdC5yYg 0KWyJ0aGlzIiwgImlzIiwgImEi
LCAidGVzdCIsICJzdHJpbmcuIiwgIm9rYXk/Il0NClsidGgiLCAicyAiLCAicyBhIHRlc3Qgc3Ry
IiwgIm5nLiBva2F5PyJdDQpbInRoaSIsICJzIGkiLCAicyBhIi wgIiB0ZSIsICJzdCAiLCAic3Ry
IiwgImluZyIsICIuIG8iLCAia2F5IiwgIj8iXQ0KDQpJcyB0aG F0IG9rPw0Ka2luZCByZWdhcmRz
IC1ib3RwDQo=

 
Reply With Quote
 
Firstname Secondname
Guest
Posts: n/a
 
      04-27-2007
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 #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



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rBATIS - alternative ORM for Ruby / Rails (via Martin Fowler) vasudevram Ruby 0 08-10-2006 04:00 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Quicktime Alternative, RealPlayer Alternative & Media Player Classic John Capleton Computer Support 3 12-05-2005 07:41 AM
[ANN] Ruby/GD2 alternative to Ruby/GD Rob Leslie Ruby 7 11-05-2005 11:04 PM
Type checking in Ruby (StrongTyping alternative). Eivind Eklund Ruby 0 11-07-2003 03:10 PM



Advertisments