Hash in Ruby & Convert python to ruby -
i using hash in ruby, check whether word in “pairs” class , replace them. code in python , want convert ruby not familiar with. here ruby code wrote.
import sys pairs = {'butter' => 'flies', 'cheese' => 'wheel', 'milk'=> 'expensive'} line in sys.stdin: line_words = line.split(" ") word in line_words: if word in pairs line = line.gsub!(word, pairs[word]) puts line
it shows following error
syntax error, unexpected kin, expecting kthen or ':' or '\n' or ';' if word in pairs ^
while below original python script right:
import sys pairs = dict() pairs = {'butter': 'flies', 'cheese': 'wheel', 'milk': 'expensive'} line in sys.stdin: line = line.strip() line_words = line.split(" ") word in line_words: if word in pairs: line = line.replace(word ,pairs[word]) print line
is because of "import sys" or “indentation”?
try this:
pairs = {'butter' => 'flies', 'cheese' => 'wheel', 'milk'=> 'expensive'} line = argv.join(' ').split(' ').map |word| pairs.include?(word) ? pairs[word] : word end.join(" ") puts line
this loop on each item passed script , return word or replacement word, joined space.
Comments
Post a Comment