xxjjnn
xxjjnn

Reputation: 15239

Ruby custom string method?

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.

Upvotes: 3

Views: 1457

Answers (3)

the Tin Man
the Tin Man

Reputation: 160551

alpha = "lorem ipsum"
alpha = alpha + "\n" + "more text"
alpha = alpha + "\n" + "look I'm woody, howdy howdy howdy"

I would like:

alpha.puts "lorem ipsum"
alpha.puts "more text"
alpha.puts "look I'm woody, howdy howdy howdy"

I would counter that you're on the wrong path; That, instead of concatenating to an string, you're really looking for an array-like behavior, where you can push content onto it, then later output it however you need at that moment.

For instance:

alpha = ["lorem ipsum"]
alpha << "more text"
alpha << "look I'm woody, howdy howdy howdy"

puts alpha
# >> lorem ipsum
# >> more text
# >> look I'm woody, howdy howdy howdy

Or:

alpha.join("\n")
# => "lorem ipsum\nmore text\nlook I'm woody, howdy howdy howdy"

If you use a string as your buffer, you're going to paint yourself into corners, especially if you need to modify that buffer later in our code. Keeping it as an array allows you to easily push/pull, shift/unshift, insert/delete or sort elements then, when everything is as you want it, simply output it.

Upvotes: 1

sawa
sawa

Reputation: 168071

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.

Upvotes: 3

Renato Zannon
Renato Zannon

Reputation: 29941

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 ;))

Upvotes: 4

Related Questions