Ruby:
שרשור מחרוזות
How to: (איך לעשות:)
Ruby makes it easy. You can use +
, <<
, or concat
. Here’s how:
# Using +
greeting = "שלום " + "עולם!"
puts greeting # => שלום עולם!
# Using <<
first_name = "ישראל"
last_name = "ישראלי"
full_name = first_name << " " << last_name
puts full_name # => ישראל ישראלי
# Using concat
hello = "שלום"
world = " עולם"
hello.concat(world)
puts hello # => שלום עולם
Deep Dive (צלילה עמוקה)
Originally, in older programming languages, strings were just arrays of characters. Concatenation was manual. But Ruby, with its user-friendly philosophy, made it much simpler.
Ruby’s +
is simple and clean, but creates a new string. The <<
and concat
methods modify the original string, which can be more efficient.
Don’t forget about interpolation:
name = "מר ישראלי"
puts "ברוך הבא, #{name}" # => ברוך הבא, מר ישראלי
Interpolation is more Ruby-ish and often preferred for its readability and performance benefits.
See Also (ראה גם)
- Ruby documentation on strings: Ruby-Doc String