What's the opposite of <<

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?

On Sat, Oct 16, 2010 at 5:23 PM, Paul R. [email protected]
wrote:

From this syntax, what must I change to take a player from the team?


Posted via http://www.ruby-forum.com/.

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)

On Oct 16, 2010, at 15:23 , Paul R. wrote:

From this syntax, what must I change to take a player from the team?
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.

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.”

On Sat, Oct 16, 2010 at 6:00 PM, Paul R. [email protected]
wrote:

=> John S.

Would it be possible to take “John” or “Smith” from name?


Posted via http://www.ruby-forum.com/.

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)

On Oct 16, 5:23pm, Paul R. [email protected] wrote:

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.

team = %w(Tom Dick Harry)
==>[“Tom”, “Dick”, “Harry”]
team << “Bob”
==>[“Tom”, “Dick”, “Harry”, “Bob”]
team.pop
==>“Bob”
team
==>[“Tom”, “Dick”, “Harry”]

But on a simple level…

say in irb mode I did the following…

name = “John”

name << " Smith"

=> John S.

Would it be possible to take “John” or “Smith” from name?

On Saturday, October 16, 2010 06:00:39 pm Paul R. wrote:

=> John S.

Would it be possible to take “John” or “Smith” from name?

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 S.”

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 C.'s method certainly works if you still have lastname lying
around…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs