lose the first character

Discussion in 'Ruby' started by Colin Summers, Jun 15, 2007.

  1. r = r.reverse.chop.reverse # remove the first character

    That works, but I bet there's a nicer method. What do people use? I
    thought there would be a pohc method (chop, in reverse).

    Thanks.

    (pickaxe is up in the other window, I see other ways, but I am curious
    if people use one clear word, like chop!)
    Colin Summers, Jun 15, 2007
    #1
    1. Advertising

  2. Check String.[] and Range

    'hhelo'[ 1 .. -1 ] # for example...

    Am 15.06.2007 um 02:35 schrieb Colin Summers:

    > r = r.reverse.chop.reverse # remove the first character
    >
    > That works, but I bet there's a nicer method. What do people use? I
    > thought there would be a pohc method (chop, in reverse).
    >
    > Thanks.
    >
    > (pickaxe is up in the other window, I see other ways, but I am curious
    > if people use one clear word, like chop!)
    >
    Florian Aßmann, Jun 15, 2007
    #2
    1. Advertising

  3. Colin Summers wrote:
    > r = r.reverse.chop.reverse # remove the first character
    >
    > That works, but I bet there's a nicer method. What do people use? I
    > thought there would be a pohc method (chop, in reverse).


    if you want to lose the first *character* try r[/./m]=""
    if you want to lose the first *byte* try r[0,1]=""

    Daniel
    Daniel DeLorme, Jun 15, 2007
    #3
  4. Daniel DeLorme wrote:
    > Colin Summers wrote:
    >> r = r.reverse.chop.reverse # remove the first character
    >>
    >> That works, but I bet there's a nicer method. What do people use? I
    >> thought there would be a pohc method (chop, in reverse).

    >
    > if you want to lose the first *character* try r[/./m]=""


    Or, non-destructively,

    r = r[/.(.*)/m,1]

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Jun 15, 2007
    #4
  5. Colin Summers

    John Joyce Guest

    On Jun 14, 2007, at 9:48 PM, Joel VanderWerf wrote:

    > Daniel DeLorme wrote:
    >> Colin Summers wrote:
    >>> r = r.reverse.chop.reverse # remove the first character
    >>>


    There are a million ways to do this in Ruby.
    Here's a way that could be readable...
    r = 'some string'
    range_end = r.length - 1
    r.slice!(1..range_end)

    We could def a method that takes a string and does all of this. One
    version could return a new string, the other could trim it in place.

    def nibble(string)
    range_end = string.length - 1
    string.slice(1..range_end)
    end

    Now we can do
    r = nibble(r)

    You could also just extend String.
    John Joyce, Jun 15, 2007
    #5
  6. Hi,

    Am Freitag, 15. Jun 2007, 23:24:29 +0900 schrieb John Joyce:
    > On Jun 14, 2007, at 9:48 PM, Joel VanderWerf wrote:
    > >Daniel DeLorme wrote:
    > >>Colin Summers wrote:
    > >>> r = r.reverse.chop.reverse # remove the first character
    > >>>

    >
    > We could def a method that takes a string and does all of this. One
    > version could return a new string, the other could trim it in place.
    >
    > def nibble(string)
    > range_end = string.length - 1
    > string.slice(1..range_end)
    > end


    Obey at least some conventions.

    class String
    def shift
    slice! 0, 1 if any?
    end
    alias lchop shift
    end

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Jun 15, 2007
    #6
  7. Colin Summers

    Trans Guest

    On Jun 14, 8:35 pm, "Colin Summers" <> wrote:
    > r = r.reverse.chop.reverse # remove the first character
    >
    > That works, but I bet there's a nicer method. What do people use? I
    > thought there would be a pohc method (chop, in reverse).


    Actually, I've been wanting a front version #chomp for a long time
    too. If I has good names I'd add them to facets.

    T.
    Trans, Jun 15, 2007
    #7
  8. Colin Summers

    Sammy Larbi Guest

    Trans wrote, On 6/15/2007 9:47 AM:
    > On Jun 14, 8:35 pm, "Colin Summers" <> wrote:
    >
    >> r = r.reverse.chop.reverse # remove the first character
    >>
    >> That works, but I bet there's a nicer method. What do people use? I
    >> thought there would be a pohc method (chop, in reverse).
    >>

    >
    > Actually, I've been wanting a front version #chomp for a long time
    > too. If I has good names I'd add them to facets.
    >
    > T.
    >
    >

    I thought the pohc! was clever. But maybe left_chop and right_chop (or
    lchop rchop) would be better suited names (or rchop for reverse chop)?
    Sammy Larbi, Jun 15, 2007
    #8
  9. Colin Summers

    John Joyce Guest

    On Jun 15, 2007, at 9:38 AM, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Freitag, 15. Jun 2007, 23:24:29 +0900 schrieb John Joyce:
    >> On Jun 14, 2007, at 9:48 PM, Joel VanderWerf wrote:
    >>> Daniel DeLorme wrote:
    >>>> Colin Summers wrote:
    >>>>> r = r.reverse.chop.reverse # remove the first character
    >>>>>

    >>
    >> We could def a method that takes a string and does all of this. One
    >> version could return a new string, the other could trim it in place.
    >>
    >> def nibble(string)
    >> range_end = string.length - 1
    >> string.slice(1..range_end)
    >> end

    >
    > Obey at least some conventions.
    >
    > class String
    > def shift
    > slice! 0, 1 if any?
    > end
    > alias lchop shift
    > end
    >
    > Bertram

    So sweet about it. What conventions do you refer to?
    I said you could add to a class, I didn't do it.
    I also was just trying to keep it clear.
    String#slice is not well documented in rdoc. ri only gives the
    formats and result types.

    ----------------------------------------------------------- String#slice
    str[fixnum] => fixnum or nil
    str[fixnum, fixnum] => new_str or nil
    str[range] => new_str or nil
    str[regexp] => new_str or nil
    str[regexp, fixnum] => new_str or nil
    str[other_str] => new_str or nil
    str.slice(fixnum) => fixnum or nil
    str.slice(fixnum, fixnum) => new_str or nil
    str.slice(range) => new_str or nil
    str.slice(regexp) => new_str or nil

    Not the most clear stuff in the world for everyone.
    especially this:
    str.slice(fixnum, fixnum) => new_str or nil

    Uh, what should those fixnums be?
    Sure we could sit around and try it in irb, but better descriptions
    would be better for rdoc and for everyone.

    shift is a pretty poor naming btw. Makes more sense if you are
    dealing with an array. Ruby's strings are not an arrays.
    You missed the OP's goal. You returned the first character.

    how about:

    # Takes a fixnum argument. Returns a new string by removing (or
    nibbling) the 'fixnum' number of bytes from the string
    class String
    def nibble(fixnum)
    range_end = self.length - 1
    slice(fixnum..range_end)
    end
    end
    John Joyce, Jun 15, 2007
    #9
    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. =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=

    session state variables across asp.net web apps... We lose them!

    =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=, Mar 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,902
    =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=
    Mar 9, 2005
  2. MS News \(MS ILM\)
    Replies:
    1
    Views:
    286
    MS News \(MS ILM\)
    Aug 22, 2003
  3. Ady

    Lose position in datagrid

    Ady, Nov 13, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    532
    Guest
    Nov 14, 2003
  4. Fraggle
    Replies:
    1
    Views:
    765
    Fraggle
    Nov 25, 2003
  5. jm
    Replies:
    2
    Views:
    298
Loading...

Share This Page