Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby different ways to run string interpolation

# TECHNIQUE 1 - standard interpolation
name = 'david'

# standard interpolation using double quiote is not lazy, it evaluates straight away
"who is #{name}"
# => who is david

# TECHNIQUE 2 - % instead of # can be used with lazy or late evalutation 
'who is %{name}' % { name: name }
# => who is david

# TECHNIQUE 3 - use eval/binding (from the Facets GEM) also supports  - % instead of # can be used with lazy or late evalutation
def interpolate(&str)
  eval "%{#{str.call}}", str.binding
end

interpolate { "who is #{name}" }
# => who is david
Comment

string interpolation ruby

# to interpolate strings in ruby use #{}

name = "Edmond"
puts "Hello, #{name}!" # outputs "Hello, Edmond!"
Comment

PREVIOUS NEXT
Code Example
Ruby :: rspec shared examples 
Ruby :: sort hash ruby 
Ruby :: rails get asset path from console 
Ruby :: autoload lib directory rails 
Ruby :: validates inclusion of rails 
Ruby :: ruby median find 
Ruby :: generate csv ruby 
Ruby :: rails link to 
Ruby :: rails render partial 
Ruby :: times ruby 
Ruby :: random datetime ruby 
Ruby :: ruby for loop 
Ruby :: Seconds to HH:MM in Ruby 
Ruby :: ruby activerecord find where less than 
Ruby :: rails console destroy all 
Ruby :: will_paginate gem rails 
Ruby :: ruby select certain keys from hash 
Ruby :: rails migration remove column 
Ruby :: object service 
Ruby :: rails migration populate data 
Ruby :: Backtracking solution in ruby 
Ruby :: rails image 
Ruby :: pick element from array that is part of params hash 
Ruby :: ruby regex replace capture group 
Ruby :: ruby match all 
Ruby :: time loop start with non zero in ruby 
Ruby :: rails callback STI 
R :: r count number of na 
R :: reverse row order dataframe R 
R :: how to eliminate duplicates in a column in r 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =