What's the opposite of <<

Discussion in 'Ruby' started by Paul Roche, Oct 16, 2010.

  1. Paul Roche

    Paul Roche Guest

    Hi, what's the opposite the concatenate sign << ?

    For example I want to do the opposite of what the following piece of
    code does...........


    player.in_squad = player.in_squad << team

    This ads a player to the team.

    From this syntax, what must I change to take a player from the team?
     
    Paul Roche, Oct 16, 2010
    #1
    1. Advertisements

  2. Paul Roche

    Josh Cheek Guest

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

    There isn't any way to tell from here, operators like << are just names of
    methods, so it can be defined however the author of the code wants. What you
    need to do is figure out what in_squad returns (p player.in_squad) and then
    look up the api for it (if it is a gem, you can do "$ gem server", then open
    your web browser to localhost:8808)
     
    Josh Cheek, Oct 16, 2010
    #2
    1. Advertisements

  3. Paul Roche

    Dave Howell Guest

    That is really strange. At first, I simply didn't look closely enough at =
    that, and "fixed" it in my head so that it actually said

    team << player.in_squad

    Which I would read as "add this player to the team." But your code looks =
    like "add this team to the player" or perhaps "give the player this team =
    on which to be."

    So I'm with Josh: in this case, "<<" does NOT mean "concatenate." You'll =
    have to read the docs or the code to figure out what it really does =
    mean, and whether or not any kind of 'opposite' method is available.=20

    Now, if the object on the left were an array, then "<<" means "append," =
    not "concatenate." "Push" also means "append," and the reverse process =
    is "pop."
     
    Dave Howell, Oct 16, 2010
    #3
  4. Paul Roche

    Paul Roche Guest

    But on a simple level......


    say in irb mode I did the following...


    name = "John"

    name << " Smith"

    => John Smith



    Would it be possible to take "John" or "Smith" from name?
     
    Paul Roche, Oct 17, 2010
    #4
  5. Paul Roche

    Josh Cheek Guest

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

    Yes, we can know this now that you say we are dealing with Strings. It would
    not work, for example, with Arrays
    name = "John"
    lastname = "Smith"
    name << lastname # => "JohnSmith"
    name.chomp! lastname
    name # => "John"

    Note that chomp! will return nil in some situations, so don't rely on
    assigning from it (if you want to do that, use the non-bang version)
     
    Josh Cheek, Oct 17, 2010
    #5
  6. Paul Roche

    w_a_x_man Guest

    team = %w(Tom Dick Harry)
    ==>["Tom", "Dick", "Harry"]
    team << "Bob"
    ==>["Tom", "Dick", "Harry", "Bob"]
    team.pop
    ==>"Bob"
    team
    ==>["Tom", "Dick", "Harry"]
     
    w_a_x_man, Oct 17, 2010
    #6
  7. Not really, now that we know it's Strings. If it was Arrays, for instance:

    name = []
    name << 'John'
    name << 'Smith'
    name.first # John
    name.last # Smith

    But since it's strings... Let me put it this way:

    name = "John Smith"

    That's basically what you just did. Now, if you assume there's a space in
    there, you can do this:

    name.split

    ...that will produce an array by splitting the string by whitespace. But that
    has nothing to do with the concatenation -- for example, if you do this:

    name = 'John'
    name << 'Smith'

    Now name is 'JohnSmith', which means you'd need to look for some other way to
    tell them apart.

    Josh Cheek's method certainly works if you still have lastname lying around...
     
    David Masover, Oct 17, 2010
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.