Control Structures: Difference between revisions

→‎[[Ruby]]: Moved to other articles.
(→‎[[Python]]: Moved to other articles.)
(→‎[[Ruby]]: Moved to other articles.)
Line 60:
 
 
==[[Ruby]]==
[[Category:Ruby]]
=== each ===
 
['foo', 'bar', 'baz'].each do |x|
puts x
end
 
=== collect ===
 
array = ['foo', 'bar', 'baz'].collect do |x|
foo x
end
 
=== map ===
 
array = ['foo', 'bar', 'baz'].map {|x| foo x }
 
=== inject ===
 
string = ['foo', 'bar', 'baz'].inject("") do |s,x|
s << x
s
end
 
sum = ['foo', 'bar', 'baz'].inject(0) do |s,x|
s + x.size
s
end
 
product = ['foo', 'bar', 'baz'].inject(1) do |p,x|
p * x.size
p
end
 
hash = ['foo', 'bar', 'baz'].inject({}) do |h,x|
h[x] = x.size
h
end
 
boolean = ['foo', 'bar', 'baz'].inject(true) do |b,x|
b &&= x != 'bar'
b
end
 
==[[Tcl]]==