Skip to content

Instantly share code, notes, and snippets.

@MaximeD
Created October 8, 2011 14:34
Show Gist options
  • Save MaximeD/1272356 to your computer and use it in GitHub Desktop.
Save MaximeD/1272356 to your computer and use it in GitHub Desktop.
many ways
#!/usr/bin/env ruby
array=[1,2,0,3,3,5,0,4]
value_to_search = 3
total = 0
# ruby one-line
array.each { |value| total += 1 if value == value_to_search}
# ruby multiple
array.each do |value|
total += 1 if value == value_to_search
end
# ruby in a perlish-way
array.each { |value|
total +=1 if value == value_to_search
}
# silly stuff
array.each do |value| total=+1 if value == value_to_search end
puts "\"#{value_to_search}\" is #{total} time(s) in array"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment