over the brown fox" puts sentence.upcase # => "THE LAZY DOG JUMPED OVER THE BROWN FOX" puts sentence.capitalize # => "The lazy dog jumped over the brown fox" class String def to_pig_latin self .split(/\s/) .map { |word| word.match(/([qwrtpdfghjklzxvbnm]*)(.*)/i) } .map { |matches| "#{matches[2]}#{matches[1]}ay" } .join(' ') end end puts sentence.to_pig_latin # => ethay azylay ogday umpedjay overay ethay ownbray oxfay
only the given keys. # Returns a hash containing the given keys. # # { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b) # # => {:a=>1, :b=>2} # def slice(*keys) if respond_to?(:convert_key, true) keys.map! { |key| convert_key(key) } end keys.each_with_object(self.class.new) do |k, hash| hash[k] = self[k] if has_key?(k) end end end Active Support
given keys. # # h = {:a=>1, :b=>2, :c=>3} # h.slice(:a, :c) #=> {:a=>1, :c=>3} # def slice(*keep_keys) if block_given? each do |k, v| keep_keys << k if yield(k, v) end end hash = {} keep_keys.each do |key| hash[key] = fetch(key) if key?(key) end hash end Core Extensions Facets
|word| word.match(/([qwrtpdfghjklzxvbnm]*)(.*)/i) } .map { |matches| "#{matches[2]}#{matches[1]}ay" } .join(' ') end end sentence = "the lazy dog jumped over the brown fox" sentence.extend(ChildSpeak) puts sentence.to_pig_latin # => ethay azylay ogday umpedjay overay ethay ownbray oxfay quote = "a rose is a rose is a rose" puts quote.to_pig_latin # => undefined method `to_pig_latin' for String
.map { |word| word.match(/([qwrtpdfghjklzxvbnm]*)(.*)/i) } .map { |matches| "#{matches[2]}#{matches[1]}ay" } .join(' ') end end end begin sentence = "the lazy dog jumped over the brown fox" puts sentence.to_pig_latin rescue Exception => error puts error.message end # => undefined method `to_pig_latin' using ChildSpeak puts sentence.to_pig_latin # => ethay azylay ogday umpedjay overay ethay ownbray oxfay
"it is a lovely day today" end def say_greeting puts greeting.to_pig_latin end end tommy = Child.new tommy.say_greeting # => itay isay aay ovelylay ayday odaytay puts tommy.greeting.to_pig_latin # => undefined method `to_pig_latin'