How do I make this code more elegant? I have:
alpha = "lorem ipsum"
alpha = alpha + "\n" + "more text"
alpha = alpha + "\n" + "look I'm woody, howdy howdy howdy"
#etc
I would like:
alpha.puts "lorem ipsum"
alpha.puts "more text"
alpha.puts "look I'm woody, howdy howdy howdy"
# etc
or, perhaps, alpha.append as it is not taken. I attempted:
class String
def append(apnd)
self.to_s + "\n" + apnd
end
end
but that doesn't modify it, only returns it. And I learned from searching you can't modify the self. I get the same problem with:
def apnd(a,b)
a = a + "\n" + b
end
which I find very confusing as I thought Ruby passes the object by reference, rather than by value.
I saw "Custom + Method in Ruby String" and a couple other Stack Overflow posts.
Update:
Aha, for the apnd method I can follow "'pass parameter by reference' in Ruby?" but that means I can't have apnd(alpha, "stuff"), it would have to be apnd(:alpha, "stuff", binding) which doesn't reduce repetition as I have to pass binding into every single method.
You can get close to what you describe using a StringIO:
require 'stringio'
alpha_io = StringIO.new
alpha_io.puts "lorem ipsum"
alpha_io.puts "more text"
alpha_io.puts "look I'm woody, howdy howdy howdy"
alpha_io.string
# => "lorem ipsum\nmore text\nlook I'm woody, howdy howdy howdy\n"
However, if your string is mostly literal, you can get away easily by using a heredoc:
alpha = <<-STRING
lorem ipsum
more text
look I'm woody, howdy howdy howdy
Some interpolation #{"here".upcase}
STRING
puts alpha
# lorem ipsum
# more text
# look I'm woody, howdy howdy howdy
# Some interpolation HERE
(Even though SO got confused with the syntax, I assure you it's valid ruby ;))
class String
def append(apnd)
concat("\n#{apnd}")
end
end
But I don't think is the right way to do it. I feel code smell in this approach. If you care about ensuring a line break when additional lines are added, then you should care about that even when additional lines are not added. In your case, the endline character should be appended to the previous string, not prepended to the string to be added. From the beginning, you should keep your strings normalized in the sense that they end with an endline character. For this, I have a method in my personal library called String#unchomp and String#unchomp!:
class String
def unchomp; sub(/#$/?\z/, $/) end
def unchomp!; sub!(/#$/?\z/, $/) end
end
Whenever I need to ensure a string ends with an endline character, I apply this method to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With