php's chunk_split alternative in ruby

Discussion in 'Ruby' started by Mister Twister, Apr 27, 2007.

  1. 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/.
     
    Mister Twister, Apr 27, 2007
    #1
    1. Advertising

  2. On Apr 27, 4:44 am, 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);
    >
    > 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}/)
     
    William James, Apr 27, 2007
    #2
    1. Advertising

  3. Mister Twister

    Alex Young Guest

    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
     
    Alex Young, Apr 27, 2007
    #3
  4. 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/.
     
    Firstname Secondname, Apr 27, 2007
    #4
  5. T24gQmVoYWxmIE9mIEZpcnN0bmFtZSBTZWNvbmRuYW1lOg0KIyBFeGFjdGx5IGxpa2UgdGhhdCA6
    KQ0KDQpBaCwgZG8gbm90IGZvcmdldCwgaW4gcnVieSBsYW5kLCB5b3UgY2FuIGJlIGZsZXhpYmxl
    IDopDQoNCkM6XGZhbWlseVxydWJ5PmNhdCAgdGVzdC5yYg0KY2xhc3MgU3RyaW5nDQogICBhbGlh
    cyBvbGRfc3BsaXQgc3BsaXQNCiAgIGRlZiBzcGxpdCBhcmcxPScgJywgKmFyZ3MNCiAgICAgIGNh
    c2UgYXJnMQ0KICAgICAgICAgd2hlbiBJbnRlZ2VyDQogICAgICAgICAgICBzZWxmLnNjYW4oLy57
    MSwje2FyZzF9fS8pDQogICAgICAgICBlbHNlDQogICAgICAgICAgICBvbGRfc3BsaXQgYXJnMSwg
    KmFyZ3MNCiAgICAgIGVuZCAjY2FzZQ0KICAgZW5kICNkZWYNCmVuZCAjY2xhc3MNCg0KcCAidGhp
    cyBpcyBhIHRlc3Qgc3RyaW5nLiBva2F5PyIuc3BsaXQNCnAgInRoaXMgaXMgYSB0ZXN0IHN0cmlu
    Zy4gb2theT8iLnNwbGl0KC9pLykNCnAgInRoaXMgaXMgYSB0ZXN0IHN0cmluZy4gb2theT8iLnNw
    bGl0KDMpDQoNCkM6XGZhbWlseVxydWJ5PnJ1YnkgdGVzdC5yYg0KWyJ0aGlzIiwgImlzIiwgImEi
    LCAidGVzdCIsICJzdHJpbmcuIiwgIm9rYXk/Il0NClsidGgiLCAicyAiLCAicyBhIHRlc3Qgc3Ry
    IiwgIm5nLiBva2F5PyJdDQpbInRoaSIsICJzIGkiLCAicyBhIiwgIiB0ZSIsICJzdCAiLCAic3Ry
    IiwgImluZyIsICIuIG8iLCAia2F5IiwgIj8iXQ0KDQpJcyB0aGF0IG9rPw0Ka2luZCByZWdhcmRz
    IC1ib3RwDQo=
     
    Peña, Botp, Apr 27, 2007
    #5
  6. 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/.
     
    Firstname Secondname, Apr 27, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. picayunish
    Replies:
    12
    Views:
    745
    picayunish
    Oct 19, 2003
  2. Rajive Narain
    Replies:
    0
    Views:
    2,084
    Rajive Narain
    Sep 18, 2009
  3. Alternative of PHP "Unset" function is ASP

    , Oct 22, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    174
    WALDO
    Oct 26, 2004
  4. Replies:
    3
    Views:
    171
    Ray Costanzo [MVP]
    Oct 22, 2004
  5. Miquel Oliete
    Replies:
    4
    Views:
    181
    Miquel Oliete
    Mar 7, 2008
Loading...

Share This Page