Skip to content

Instantly share code, notes, and snippets.

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
# 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 "::".
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
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
puts '==========='
# Same string content - different objects
puts "ZigZag".object_id #=> 25986960
puts '==========='
puts "ZigZag".object_id #=> 25986940
puts '==========='
:mysymbol = 'ZigZag'
# symbols03.rb:1: syntax error, unexpected '=', expecting $end :mysymbol = 'ZigZag'
# ^
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 '==========='
: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
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'
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