Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby string interpolation lazy evaluation

name = 'david'

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

# using single quote does not evaluate 
'who is #{name}'
# => who is #{name}

# TECHNIQUE 1 - % instead of #
'who is %{name}' % { name: name }
# => who is david

# TECHNIQUE 2 - use eval/binding (from the Facets GEM)
def interpolate(&str)
  eval "%{#{str.call}}", str.binding
end

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

PREVIOUS NEXT
Code Example
Ruby :: ruby get haft of array 
Ruby :: ruby timeout 
Ruby :: compiler version at runtime 
Ruby :: how to run one line pry 
Ruby :: simpleCov formatter set two formats 
Ruby :: ruby read file line by line 
Ruby :: i am working in ruby 2.6 how to jump to a lower version 
R :: r sort character number 
R :: Drop rows with missing values in R 
R :: generate all possible combinations of a set of characters r 
R :: r list append 
R :: read csv in r 
R :: merge multiple datatable in r 
R :: set row names in r 
R :: r language comment 
R :: combine columns in r 
R :: r read excel start from row 
R :: turn row names into column in r 
R :: R dplyr select 
R :: table() in r 
R :: %in% r 
R :: ggplot categorical data r 
R :: how to add columns to a flextable in r 
R :: formatc in r 
R :: select last child with class in r 
R :: rstudio refactor hotkey 
R :: sumif in r 
R :: emf from r plot 
Rust :: rust filtering a vector example 
Rust :: ..= in rust 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =