What is that method called? - RUBY

This is a discussion on What is that method called? - RUBY ; I need the method that has the ability to turn a long string like this: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus elementum lacinia. Ut aliquam ...

+ Reply to Thread
Results 1 to 8 of 8

What is that method called?

  1. Default What is that method called?

    I need the method that has the ability to turn a long string like this:

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus
    elementum lacinia. Ut aliquam vehicula sem.

    into a shorter string like this:

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    pulvinar turpis...

    My problem is that I can't remember what the method is called. I looked
    through the whole Ruby String documentation
    (http://ruby-doc.org/core/classes/String.html) but I couldn't find it.
    Can anyone remember what it's called?
    --
    Posted via http://www.ruby-forum.com/.


  2. Default Re: What is that method called?

    David Trasbo wrote:
    > I need the method that has the ability to turn a long string... into a shorter string...
    > My problem is that I can't remember what the method is called. I looked
    > through the whole Ruby String documentation...


    There's no such method in Ruby. You might be thinking of the
    truncate method that's part of ActionView's TextHelper module.

    Clifford Heath.

  3. Default Re: What is that method called?

    I seriously doubt this is part of standard Ruby.

    You have probably seen this in ActiveSupport and/or the 'english' gem
    - English::String#brief:

    http://english.rubyforge.org/rdoc/cl...g.html#M000096

    Cheers,
    Peter

    On 2008.11.11., at 10:16, David Trasbo wrote:

    > I need the method that has the ability to turn a long string like
    > this:
    >
    > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    > pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus
    > elementum lacinia. Ut aliquam vehicula sem.
    >
    > into a shorter string like this:
    >
    > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    > pulvinar turpis...
    >
    > My problem is that I can't remember what the method is called. I
    > looked
    > through the whole Ruby String documentation
    > (http://ruby-doc.org/core/classes/String.html) but I couldn't find it.
    > Can anyone remember what it's called?
    > --
    > Posted via http://www.ruby-forum.com/.
    >




  4. Default Re: What is that method called?

    Peter Szinek wrote:
    > I seriously doubt this is part of standard Ruby.


    You're right, it's not a part of the Ruby standard library:

    Clifford Heath wrote:
    > There's no such method in Ruby. You might be thinking of the
    > truncate method that's part of ActionView's TextHelper module.


    truncate is what it's called. I was actually searching for 'rails
    StringHelper'...

    Thanks for the replies!
    --
    Posted via http://www.ruby-forum.com/.


  5. Default Re: What is that method called?

    [Note: parts of this message were removed to make it a legal post.]

    David Trasbo wrote:
    > Peter Szinek wrote:
    >
    >> I seriously doubt this is part of standard Ruby.
    >>

    >
    > You're right, it's not a part of the Ruby standard library:
    >
    > Clifford Heath wrote:
    >
    >> There's no such method in Ruby. You might be thinking of the
    >> truncate method that's part of ActionView's TextHelper module.
    >>

    >
    > truncate is what it's called. I was actually searching for 'rails
    > StringHelper'...
    >
    > Thanks for the replies!
    >


    Also:

    a_long_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing
    elit. Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae
    pede nec lacus elementum lacinia. Ut aliquam ehicula sem."
    limit = 86
    a_long_string[0, limit] << "..." #=> "Lorem ipsum dolor sit amet,
    consectetuer adipiscing elit. Pellentesque pulvinar turpis..."


    -Justin


  6. Default Re: What is that method called?

    perhaps this works?




    string = <<END
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus
    elementum lacinia. Ut aliquam vehicula sem.
    END

    lead = 90

    wordlist = string.split(' ')

    shorttext = ''

    for word in wordlist

    break if(shorttext.length + word.length > lead)
    shorttext = shorttext + word + " "

    end

    # get rid of the last space
    shorttext.chop!

    puts "#{shorttext}..."
    --
    Posted via http://www.ruby-forum.com/.


  7. Default Re: What is that method called?

    Justin Collins <justincollins@ucla.edu> writes:

    > [Note: parts of this message were removed to make it a legal post.]
    >
    > David Trasbo wrote:
    >> Peter Szinek wrote:
    >>
    >>> I seriously doubt this is part of standard Ruby.
    >>>

    >>
    >> You're right, it's not a part of the Ruby standard library:
    >>
    >> Clifford Heath wrote:
    >>
    >>> There's no such method in Ruby. You might be thinking of the
    >>> truncate method that's part of ActionView's TextHelper module.
    >>>

    >>
    >> truncate is what it's called. I was actually searching for 'rails
    >> StringHelper'...
    >>
    >> Thanks for the replies!
    >>

    >
    > Also:
    >
    > a_long_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing
    > elit. Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae
    > pede nec lacus elementum lacinia. Ut aliquam ehicula sem."
    > limit = 86
    > a_long_string[0, limit] << "..." #=> "Lorem ipsum dolor sit amet,
    > consectetuer adipiscing elit. Pellentesque pulvinar turpis..."


    In general, for input strings less than the limit, you wouldn't want "..."

    class String
    # Return an ellided string no longer than total_length
    # "hello".ellide(5) -> "hello"
    # "hello there".ellide(8) -> "hello..."
    def ellide total_length
    if self.length > total_length
    raise 'total_length must be at least 3' if total_length < 3
    return self[0, total_length - 3] + '...'
    else
    return self
    end
    end
    end

    >
    > -Justin
    >


    --
    Brian Adkins
    http://www.lojic.com/
    http://lojic.com/blog/

  8. Default Re: What is that method called?

    Boris Blaadh <nejnejnej@gmail.com> writes:

    > perhaps this works?
    >
    > string = <<END
    > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque
    > pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus
    > elementum lacinia. Ut aliquam vehicula sem.
    > END
    >
    > lead = 90
    >
    > wordlist = string.split(' ')
    >
    > shorttext = ''
    >
    > for word in wordlist
    >
    > break if(shorttext.length + word.length > lead)
    > shorttext = shorttext + word + " "
    >
    > end
    >
    > # get rid of the last space
    > shorttext.chop!
    >
    > puts "#{shorttext}..."


    It is nicer to break at word boundaries in some contexts. But if it's
    a single word (such as a long email address), then you'd still want to
    show something more than "...". Maybe you can add a check for that.

    e.g. for string = 'john.smith@hisdomain.com', lead = 15

    --
    Brian Adkins
    http://www.lojic.com/
    http://lojic.com/blog/

+ Reply to Thread