[chef] Re: Where does the new line symbol is ?


Chronological Thread 
  • From: Kendall Gifford < >
  • To:
  • Subject: [chef] Re: Where does the new line symbol is ?
  • Date: Fri, 4 Oct 2013 11:21:13 -0600

On Fri, Oct 4, 2013 at 5:57 AM, Vladimir Skubriev < " target="_blank"> > wrote:
I have this code:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.chomp

The "chomp" in the line of code above has no effect. The problem is that it is being applied to the literal string: '\1' which has no newline character in it.

If you want it to apply to the string that is the result of the call to the #sub method, you need some parenthesis:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub(/.*\((.*)\).*/, '\1').chomp

Another way to get rid of the newline character that comes from the "gem list | grep ..." command is to alter your regular _expression_ so that the "." (dot) character will also match newline characters, my adding the "m" modifier:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/m, '\1'

Anyhow, this explains why the pasver variable holds a string that includes a newline.
 

if ! ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}/buildout/apache2/mod_passenger.so") then

    bash "build_and_install_passenger_module_for_apache" do
    user "root"
    code <<-EOH
                source /etc/profile.d/rvm.sh
/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/bin/passenger-install-apache2-module -a
    EOH
    end
end

And a problem is that "/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}/buildout/apache2/mod_passenger.so" if a two lines string variable

Why this happens ?

The code "pasver.chomp!" should be removing that newline character successfully. I don't know why there would still be a newline character. However, if, somehow, the pasver string was terminated with more than one newline character (2, for example) that would explain why #chomp! is failing to remove it. A ruby example in ruby's irb shell:

$ irb
> "test\n\n".chomp!
=> "test\n"

Note: you can just use "chomp" instead of "chomp!" unless you use "pasver" later and expect it to already have newlines removed.
 

I have second use case in other (erb) file:

<% pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.chomp %>
PassengerRoot <%= "/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}" %>
PassengerDefaultRuby /usr/local/rvm/wrappers/<%= node['redmine']['rubyversion']%>/ruby

This file generates:

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19
/buildout/apache2/mod_passenger.so

How does a template with "Passenger Root ..." and "PassengerDefaultRuby ..." create a file with "LoadModule ..." ?
 

Why ?

Where does the new line symbol is ?


Again, I'm not sure why the call to #chomp! isn't removing the trailing newline (again, unless there are somehow multiple newlines).

However, in both cases, fixing your call to #sub by either including the "m" modifier, and/or changing the regular _expression_ itself will result in "pasver" _not_ having any trailing newlines in the first place, making it unnecessary to call #chomp! or #chomp at all.




Archive powered by MHonArc 2.6.16.

§