In an electromagnet, if the electric coil induces a magnetic field in the metal bar with the same polarity as the one in the coil, should not the bar and coil repel one another, "like poles repel"?
When the bar is not fully inside, the magnetic field of the coil magnetizes the bar in such a way that the bar end closest to the coil has opposite pole to the nearest pole of the coil. The force between these two is attractive.
----------- -------- bar S | N coil -------- ----------- --> <-- force on bar force on coil
For example, when the bar approaches the coil near the N pole of the coil, the closest bar end becomes an S pole. There is also repulsive force on this pole from the other coil pole, but this repulsive force is weaker because of greater distance.
When the bar gets inside the coil, we know it is still being attracted and pulled inside the coil, despite the attracted poles getting farther from each other. Things become a little harder to describe, because the dipole model of magnet and coil is no longer sufficient. The magnetic field of the coil inside the coil is almost uniform, which means it acts with close to zero force on the part of the magnet that is already inside. It only acts with strong force on the part that still outside. So, the pulling continues until the bar is centered at the coil's center.
The total attractive force becomes zero when the bar is fully inside the coil, because then due to symmetry if the bar moves, the attractive forces pull it back. The only effect that remains is tension that pulls the elements of the bar in radial directions, perpendicular to its mantle, to expand it. But this effect is weak and is usually not visible.
The best answer I could find is that
A magnetic pole is just the location where field lines leave (North) or enter (South) a magnet. A hollow magnet therefor has internal poles such that again the good old rule applies that unlike poles attract and likes repel.
So the metal bar is actually attracted to the solenoid, rather than repelled, and that principle is used in coil guns:
Feel free to correct this if there is a better explanation.