This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_from_user_input | |
puts "Please insert a year to see if it's a Leap year: " | |
STDOUT.flush | |
gets.chomp | |
end | |
def validator(a_string) | |
if(x=(a_string.scan(/\D/).size==0)) | |
puts "numeric" | |
puts a_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# scope_dbl_deny.rb | |
=begin | |
This code comes from a discussion in rails-br group. | |
http://groups.google.com/group/rails-br/browse_thread/thread/775784b8e759721f?hl=pt-BR | |
It is all about two newbie questions: | |
One about scope operator "::". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def safe?(col, others) | |
others.each_with_index do |c, r| | |
return false if col == c or (col - c).abs == (others.size - r).abs | |
end | |
end | |
def solve(n, solutions = [], cols=[]) | |
return solutions.push(cols) unless cols.size < n | |
n.times { |col| solve(n, solutions, cols + [col]) if safe?(col, cols) } | |
return solutions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts '===========' | |
FIXED = 'MROS' | |
puts FIXED | |
puts FIXED.object_id #=> 25986580 | |
puts '===========' | |
# after a while you may have a more flexible constant | |
FIXED = 'MarcRic' | |
# Ruby only show you a warning: already initialized constant FIXED | |
puts FIXED | |
puts FIXED.object_id #=> 25986550 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts '===========' | |
# Same string content - different objects | |
puts "ZigZag".object_id #=> 25986960 | |
puts '===========' | |
puts "ZigZag".object_id #=> 25986940 | |
puts '===========' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:mysymbol = 'ZigZag' | |
# symbols03.rb:1: syntax error, unexpected '=', expecting $end :mysymbol = 'ZigZag' | |
# ^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts '===========' | |
puts :ZigZag.object_id #=> 199098 | |
puts :ZigZag.object_id #=> 199098 | |
puts '===========' | |
puts "ZigZag".to_sym.object_id #=> 199098 | |
puts "ZigZag".to_sym.object_id #=> 199098 | |
puts '===========' | |
puts :ZigZag == :ZigZag | |
# We get this true due to just one comparision | |
puts '===========' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:tynysymbol | |
$global_var = 'Zero Level' | |
puts :tynysymbol.object_id | |
class TestFirstClass | |
@@class_var1 = 'First Level Class' | |
def testFirstMethod | |
@method_var1 = 'First Level Method' | |
puts $global_var | |
puts @@class_var1 | |
puts @method_var1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = ['eeny','meeny','miny','moe'] | |
puts '===========' | |
s.each do |v| | |
myvar = v | |
puts myvar | |
puts myvar.object_id | |
end | |
puts '===========' | |
s.each do |v| | |
myvar = 'Constant' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
string_test = Benchmark.measure do | |
6_000_000.times do | |
"test" | |
end | |
end.total | |
symbol_test = Benchmark.measure do | |
6_000_000.times do |
OlderNewer