What's the opposite of <<

P

Paul Roche

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?
 
J

Josh Cheek

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

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?
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)
 
D

Dave Howell

Hi, what's the opposite the concatenate sign << ?
=20
For example I want to do the opposite of what the following piece of
code does...........
=20
=20
player.in_squad =3D player.in_squad << team
=20
This ads a player to the team.
=20
=46rom 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.=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."
 
P

Paul Roche

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?
 
J

Josh Cheek

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

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?
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)
 
W

w_a_x_man

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"]
 
D

David Masover

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?

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top