Newby question 'unless'

Discussion in 'Ruby' started by EdUarDo, Jul 25, 2005.

  1. EdUarDo

    EdUarDo Guest

    Hi all, I'm overriding to_s method and I want to
    print an attribute if a condition is fulfilled.

    def to_s
    "#{@a}" + "#{@b}" unless @a == 0
    end

    That sentence doesn't print anything when @a == 0 but what I want to do
    is to not print @b when @a == 0.

    What's the correct syntax?

    I know I could do:

    def to_s
    s = "\n#{@post}"
    if @post != :GK
    s += "#{@side}"
    end
    s
    end
    EdUarDo, Jul 25, 2005
    #1
    1. Advertising

  2. EdUarDo

    Belorion Guest

    On 7/25/05, EdUarDo <> wrote:
    > Hi all, I'm overriding to_s method and I want to
    > print an attribute if a condition is fulfilled.
    >=20
    > def to_s
    > "#{@a}" + "#{@b}" unless @a =3D=3D 0
    > end
    >=20
    > That sentence doesn't print anything when @a =3D=3D 0 but what I want to =

    do
    > is to not print @b when @a =3D=3D 0.
    >=20
    > What's the correct syntax?
    >=20
    > I know I could do:
    >=20
    > def to_s
    > s =3D "\n#{@post}"
    > if @post !=3D :GK
    > s +=3D "#{@side}"
    > end
    > s
    > end


    You could try the ternary operator:

    def to_s
    @a =3D=3D 0 ? "#{@a}" : "#{@a} #{@b}"
    end

    Or,

    def to_s
    s =3D @a
    s << " #{@b}" unless @a =3D=3D 0
    return s
    end
    end
    Belorion, Jul 25, 2005
    #2
    1. Advertising

  3. Hi,

    > def to_s
    > "#{@a}" + "#{@b}" unless @a == 0
    > end


    def to_s
    "#{@a}" + (@a == 0 ? "" : "#{@b}" )
    end


    > def to_s
    > s = "\n#{@post}"
    > if @post != :GK
    > s += "#{@side}"


    s += creates a new object. Doing << on strings is much faster.


    Patrick
    Patrick Gundlach, Jul 25, 2005
    #3
  4. Hi --

    On Tue, 26 Jul 2005, EdUarDo wrote:

    > Hi all, I'm overriding to_s method and I want to
    > print an attribute if a condition is fulfilled.
    >
    > def to_s
    > "#{@a}" + "#{@b}" unless @a == 0
    > end
    >
    > That sentence doesn't print anything when @a == 0 but what I want to do
    > is to not print @b when @a == 0.


    If it doesn't print anything then presumably it's not printing @b. Or
    did you mean that last @a to be @b? If so, you could do:

    def to_s
    "#{@a unless @a.zero?}#{@b unless @b.zero?}"
    end


    David

    --
    David A. Black
    David A. Black, Jul 25, 2005
    #4
  5. Hi --

    On Tue, 26 Jul 2005, David A. Black wrote:

    > Hi --
    >
    > On Tue, 26 Jul 2005, EdUarDo wrote:
    >
    >> Hi all, I'm overriding to_s method and I want to
    >> print an attribute if a condition is fulfilled.
    >>
    >> def to_s
    >> "#{@a}" + "#{@b}" unless @a == 0
    >> end
    >>
    >> That sentence doesn't print anything when @a == 0 but what I want to do
    >> is to not print @b when @a == 0.

    >
    > If it doesn't print anything then presumably it's not printing @b. Or
    > did you mean that last @a to be @b? If so, you could do:
    >
    > def to_s
    > "#{@a unless @a.zero?}#{@b unless @b.zero?}"
    > end


    I think I figured out what you want :) You always want @a, and you
    only want @b if @a is not zero:

    "#{@a}#{@b unless @a.zero?}"


    David

    --
    David A. Black
    David A. Black, Jul 25, 2005
    #5
  6. On Jul 25, 2005, at 10:15 AM, EdUarDo wrote:

    > Hi all, I'm overriding to_s method and I want to
    > print an attribute if a condition is fulfilled.
    >
    > def to_s
    > "#{@a}" + "#{@b}" unless @a == 0
    > end


    Just FYI, It's better just to include that in one string:

    def to_s
    "#{@a}#{@b}" unless @a.zero?
    end

    > That sentence doesn't print anything when @a == 0 but what I want
    > to do
    > is to not print @b when @a == 0.
    >
    > What's the correct syntax?


    def to_s
    if @a.zero?
    @a.to_s
    else
    "#{@a}#{@b}"
    end
    end

    Hope that helps.

    James Edward Gray II
    James Edward Gray II, Jul 25, 2005
    #6
  7. EdUarDo

    EdUarDo Guest

    > def to_s
    > s = @a
    > s << " #{@b}" unless @a == 0
    > return s
    > end


    I'll take this one, in this case the ternary operator
    introduces a duplicity in the code that I don't like.
    EdUarDo, Jul 25, 2005
    #7
  8. EdUarDo

    EdUarDo Guest

    > I think I figured out what you want :) You always want @a, and you
    > only want @b if @a is not zero:


    Yes :)

    >
    > "#{@a}#{@b unless @a.zero?}"


    That's what I wanted. I didn't know that the condition could be into
    the brackets. Thanks.
    EdUarDo, Jul 25, 2005
    #8
  9. Hi --

    On Tue, 26 Jul 2005, Patrick Gundlach wrote:

    > Hi,
    >
    >> def to_s
    >> "#{@a}" + "#{@b}" unless @a == 0
    >> end

    >
    > def to_s
    > "#{@a}" + (@a == 0 ? "" : "#{@b}" )
    > end


    Conditionals give you nil when they're not met, and nil.to_s is an
    empty string, so you don't have to specify a literal empty string in
    cases like this. I would refactor your second line to:

    "#{@a}" + ("#{@b}" unless @a.zero?)

    at which point you can make it nicer by getting rid of the parens and
    the + :

    "#{@a}#{@b unless @a.zero?}"

    That gives you a closer lexical mapping between what you're asking for
    and how the eventually string will read ("@a@b?", one might say).


    David

    --
    David A. Black
    David A. Black, Jul 25, 2005
    #9
  10. Hello David,

    > Conditionals give you nil when they're not met, and nil.to_s is an
    > empty string, so you don't have to specify a literal empty string in
    > cases like this.


    Sure, but not in the case I posted:

    > def to_s
    > "#{@a}" + (@a == 0 ? "" : "#{@b}" )
    > end



    -:4:in `+': cannot convert nil into String (TypeError)
    from -:4:in `to_s'
    from -:8


    > "#{@a}#{@b unless @a.zero?}"


    This is indeed much better. Thanks for your advice - I can see my ruby
    skills getting better every day.

    Patrick
    Patrick Gundlach, Jul 25, 2005
    #10
  11. Hi --

    On Tue, 26 Jul 2005, Patrick Gundlach wrote:

    > Hello David,
    >
    >> Conditionals give you nil when they're not met, and nil.to_s is an
    >> empty string, so you don't have to specify a literal empty string in
    >> cases like this.

    >
    > Sure, but not in the case I posted:
    >
    >> def to_s
    >> "#{@a}" + (@a == 0 ? "" : "#{@b}" )
    >> end

    >
    >
    > -:4:in `+': cannot convert nil into String (TypeError)


    Whoops, I thought I was being helpful by showing an intermediate step
    but indeed that + nil would not work :)


    David

    --
    David A. Black
    David A. Black, Jul 25, 2005
    #11
  12. EdUarDo <> wrote:
    > > I think I figured out what you want :) You always want @a, and you
    > > only want @b if @a is not zero:

    >
    > Yes :)
    >
    > >
    > > "#{@a}#{@b unless @a.zero?}"

    >
    > That's what I wanted. I didn't know that the condition could be into
    > the brackets. Thanks.


    #{} can contain an arbitrarily complex expression, the result of which
    has to_s called on it before being interpolated.

    martin
    Martin DeMello, Jul 26, 2005
    #12
    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. Hermann W Ehlers

    Newby question

    Hermann W Ehlers, May 2, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    690
    Matt Berther
    May 5, 2004
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    ASP.NET easy newby question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    457
    Kevin Spencer
    Nov 23, 2004
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    634
    Iain Norman
    Feb 16, 2005
  4. Gábor SEBESTYÉN

    Unless unless

    Gábor SEBESTYÉN, Jun 17, 2005, in forum: Ruby
    Replies:
    3
    Views:
    138
    Gábor SEBESTYÉN
    Jun 17, 2005
  5. Jason C
    Replies:
    3
    Views:
    245
    Peter J. Holzer
    Nov 3, 2012
Loading...

Share This Page