Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby format string with parameters

# Rubocop issues with string formating
# Examples will print key/value pairs to console with padding for key
# => "name                 : value goes here"

# Cop success:
puts format('%-20<key>s : %<value>s', key: 'name', value: name)

# Cop failure: Style/FormatString: Favor format over String#%
puts '%-20s : %s' % ['name', name]

# Cop failure: Style/FormatStringToken: Prefer annotated tokens (like %<foo>s) over unannotated tokens (like %s)
puts format('%-20s : %s', 'name', name)
Comment

ruby string format

time    = 5
message = "Processing of the data has finished in %d seconds" % [time]
puts message
Output => "Processing of the data has finished in 5 seconds"

score = 78.5431
puts "The average is %0.2f" % [score]
Output => The average is 78.54

puts "122 in HEX is %x" % [122]
Output => 122 in HEX is 7a

puts "The number is %04d" % [20]
Output => The number is 0020

names_with_ages = [["john", 20], ["peter", 30], ["david", 40], ["angel", 24]]
names_with_ages.each { |name, age| puts name.ljust(10) + age.to_s }
# Prints the following table
john      20
david     30
peter     40
angel     24
Comment

string formattion ruby

name1 = "John"
name2 = "Mary"
"hello, #{name1}.  Where is #{name2}?"
Comment

PREVIOUS NEXT
Code Example
Ruby :: install ruby on rails ubuntu 18.04 
Ruby :: rails pass params in url 
Ruby :: ruby on rails examples 
Ruby :: check if the substring is present in the column of the record rails console 
Ruby :: how to add variable inside string ruby 
Ruby :: for loop with condition in ruby 
Ruby :: how to unloack user devise rails 
Ruby :: rails active storage get all attachment names 
Ruby :: ruby named parameters 
Ruby :: ruby nil to float is 0.00 
Ruby :: ruby execute code in string 
Ruby :: how to pass locals in rails partial 
Ruby :: add key and value to first spot in hash ruby 
Ruby :: self referencing association in ruby on rails 
Ruby :: ruby classes 
Ruby :: ruby basic arithmetic 
Ruby :: cloudbuild ruby googl 
Ruby :: ruby on rails project 
R :: r ggplot regression line 
R :: how to set the first column as row names in r 
R :: r convert list to comma separated string 
R :: sort dataframe r 
R :: change from matrix to a dataframe in r 
R :: null count in r 
R :: how to use ifelse in r 
R :: reorder factors in r 
R :: R language get all columns in a dataset 
R :: how to iterate through a list in r 
R :: how to rename variables in r dplyr 
R :: r code mutate 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =