Examples
The following examples can be run in a Ruby shell such as Interactive Ruby Shell or saved in a file and run from the command line by typing ruby
.
Classic Hello world example:
puts "Hello World!"Some basic Ruby code:
# Everything, including a literal, is an object, so this works: -199.abs # 199 "ice is nice".length # 11 "ruby is cool.".index("u") # 1 "Nice Day Isn't It?".downcase.split("").uniq.sort.join # " '?acdeinsty"Conversions:
puts "What's your favorite number?" number = gets.chomp output_number = number.to_i + 1 puts output_number.to_s + ' is a bigger and better favorite number.'Read more about this topic: Ruby (programming Language)
Famous quotes containing the word examples:
“No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.”
—André Breton (18961966)
“It is hardly to be believed how spiritual reflections when mixed with a little physics can hold peoples attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.”
—G.C. (Georg Christoph)
“In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.”
—Michel de Montaigne (15331592)