Ruby: Merging variables in to a string

前端 未结 7 929
[愿得一人]
[愿得一人] 2020-12-02 10:23

I\'m looking for a better way to merge variables into a string, in Ruby.

For example if the string is something like:

\"The animal action<

相关标签:
7条回答
  • 2020-12-02 10:36

    The standard ERB templating system may work for your scenario.

    def merge_into_string(animal, second_animal, action)
      template = 'The <%=animal%> <%=action%> the <%=second_animal%>'
      ERB.new(template).result(binding)
    end
    
    merge_into_string('tiger', 'deer', 'eats')
    => "The tiger eats the deer"
    
    merge_into_string('bird', 'worm', 'finds')
    => "The bird finds the worm"
    
    0 讨论(0)
  • 2020-12-02 10:38

    You can use sprintf-like formatting to inject values into the string. For that the string must include placeholders. Put your arguments into an array and use on of these ways: (For more info look at the documentation for Kernel::sprintf.)

    fmt = 'The %s %s the %s'
    res = fmt % [animal, action, other_animal]  # using %-operator
    res = sprintf(fmt, animal, action, other_animal)  # call Kernel.sprintf
    

    You can even explicitly specify the argument number and shuffle them around:

    'The %3$s %2$s the %1$s' % ['cat', 'eats', 'mouse']
    

    Or specify the argument using hash keys:

    'The %{animal} %{action} the %{second_animal}' %
      { :animal => 'cat', :action=> 'eats', :second_animal => 'mouse'}
    

    Note that you must provide a value for all arguments to the % operator. For instance, you cannot avoid defining animal.

    0 讨论(0)
  • 2020-12-02 10:38

    I would use the #{} constructor, as stated by the other answers. I also want to point out there is a real subtlety here to watch out for here:

    2.0.0p247 :001 > first_name = 'jim'
     => "jim" 
    2.0.0p247 :002 > second_name = 'bob'
     => "bob" 
    2.0.0p247 :003 > full_name = '#{first_name} #{second_name}'
     => "\#{first_name} \#{second_name}" # not what we expected, expected "jim bob"
    2.0.0p247 :004 > full_name = "#{first_name} #{second_name}"
     => "jim bob" #correct, what we expected
    

    While strings can be created with single quotes (as demonstrated by the first_name and last_name variables, the #{} constructor can only be used in strings with double quotes.

    0 讨论(0)
  • 2020-12-02 10:40

    This is called string interpolation, and you do it like this:

    "The #{animal} #{action} the #{second_animal}"
    

    Important: it will only work when string is inside double quotes (" ").

    Example of code that will not work as you expect:

    'The #{animal} #{action} the #{second_animal}'
    
    0 讨论(0)
  • 2020-12-02 10:47

    The idiomatic way is to write something like this:

    "The #{animal} #{action} the #{second_animal}"
    

    Note the double quotes (") surrounding the string: this is the trigger for Ruby to use its built-in placeholder substitution. You cannot replace them with single quotes (') or the string will be kept as is.

    0 讨论(0)
  • 2020-12-02 10:54

    You can use it with your local variables, like this:

    @animal = "Dog"
    @action = "licks"
    @second_animal = "Bird"

    "The #{@animal} #{@action} the #{@second_animal}"

    the output would be: "The Dog licks the Bird"

    0 讨论(0)
提交回复
热议问题