Ruby 1.9
Version of implementation Ruby of programming language RubyFull list of changes: http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l4 http://slideshow.rubyforge.org/ruby19.html
Examples:
Factorial - Ruby (194):
This example uses an iterative factorial definition
def fact(n)
(1..n).reduce(:*)
end
(0..16).each {|x| puts "#{x}! = #{fact(x)}"}
Quadratic equation - Ruby (290):
puts 'A = '
A = gets.chomp.to_f
if (A == 0)
puts 'Not a quadratic equation.'
return
end
puts 'B = '
B = gets.chomp.to_f
puts 'C = '
C = gets.chomp.to_f
D = B*B - 4*A*C
if (D == 0)
puts 'x = '+(-B/2/A).to_s
else
if (D > 0)
puts 'x1 = '+((-B-Math.sqrt(D))/2/A).to_s
puts 'x2 = '+((-B+Math.sqrt(D))/2/A).to_s
else
puts 'x1 = ('+(-B/2/A).to_s+','+(Math.sqrt(-D)/2/A).to_s+')'
puts 'x2 = ('+(-B/2/A).to_s+','+(-Math.sqrt(-D)/2/A).to_s+')'
end
end
CamelCase - Ruby (291):
-
Read the string from standard input (
gets.chomp
) - Split the string into parts, separated with matches of regular expression, which describes a sequence of one or more non-letter characters
-
Apply to each part function
capitalise
-
Concatenate the resulting strings (
join
) and print the result (puts
)
puts gets.chomp.split( /[^a-zA-Z]+/ ).map {|w| w.capitalize}.join
CamelCase - Ruby (292):
This works in a same way as this example, but scan
extracts the matches of regular expression instead of discarding them and extracting parts of string between them, as split
does.
puts gets.chomp.scan( /[a-zA-Z]+/ ).map {|w| w.capitalize}.join
Comments
]]>blog comments powered by Disqus
]]>