Why Word 'Translate' Is Messing Irb

Why word 'translate' is messing irb?

It is a bug in YARV that was fixed in YARV 2.4.0.

The commit message mentions the following workaround if you don't have YARV 2.4.0:

class << RubyVM::InstructionSequence
def translate; end
undef translate
end

Note that other implementations are not affected, only YARV.

What is translate keyword do in Ruby

Each Ruby syntax highlighting library often includes common phrases that are used in things like Rails. For example, belongs_to, while not a special keyword in a Ruby sense, is very common in Rails applications so it's often highlighted.

translate might be a special phrase as well as it's used by a lot of I18N libraries.

The only way to find out for sure is to look at the rules for syntax highlighting your editor uses. Usually there's a list of special method names in there.

Ruby doesn't recognize `downcase` or `split` as defined methods?

If returns a blank string because you don't return anything inside the collect! block. Return word and it will work :

def translate phrase
phrase = phrase.downcase.split(/ /)
phrase.collect! do |word|
word = word.split(//)
switched = false
while switched == false
word.each_index do |letter|
if word[letter] == ("a" || "e" || "i" || "o" || "u")
switched = true
word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
end
end
end
word
end
return phrase.join(" ")
end

puts translate("chocolate cream")
#=> atechocolay amcreay

It doesn't look like it's returning exactly what you expected, but it's still better than a blank string.

As for your weird error message in the console, it seems to be specific to the REPL (possibly because of the method name translate).

How to parse week and year with strptime

I think that you need to use the commercial method.

require 'date'

week = 20
start_week = Date.commercial(2020, week)
# => Mon, 11 May 2020

puts "#{start_week}"
# => 2020-05-11

In case you need a Range, you can do:

Date.commercial(2020, week).all_week
# => Mon, 11 May 2020..Sun, 17 May 2020

For more information see the documentation.



Related Topics



Leave a reply



Submit