Skip to content

Instantly share code, notes, and snippets.

View wnuqui's full-sized avatar

Wilfrido Nuqui Jr. wnuqui

  • Mandaluyong, Philippines
View GitHub Profile
@wnuqui
wnuqui / gist:2651356
Created May 10, 2012 06:16
method chain
class A
def b
data = [1, 2, 3]
def data.foo!
self.sample
end
data
end
end
@wnuqui
wnuqui / gist:2652404
Created May 10, 2012 10:53
Put your constant at the bottom
class A
def self.constant
[1, 2, 3]
end
B = constant and nil
end
@wnuqui
wnuqui / gist:2666644
Created May 12, 2012 13:57
safe conversions instead of to_x
i = "1i"
i.to_i # => 1
Integer(i) # => ArgumentError: invalid value for Integer(): "1i"
f = "1.2f"
f.to_f # => 1.2
Float(f) # => ArgumentError: invalid value for Float(): "1.2f"
@wnuqui
wnuqui / gist:2666669
Created May 12, 2012 14:02
What is your quick guess for the 2nd line?
# send following line to irb and see the result
a = 1, b = 2*5
# perhaps this is better
a = 1, (b = 2*5)
@wnuqui
wnuqui / gist:2715883
Created May 17, 2012 02:58
prompt in rake task definition
# _thanks_ to where I saw this
task :prompt do
STDOUT.puts "Prompt? "
prompt = STDIN.gets.chomp
STDOUT.puts "Cool! Prompt is #{prompt}."
end
@wnuqui
wnuqui / gist:2717282
Created May 17, 2012 07:59
where ruby's str#gsub can really prove that assumptions can really go wrong (any light?)
# ruby is 1.9.2p290
"Foo\n".gsub('\n', '') # => "Foo\n"
"Foo\n".gsub("\n", '') # => "Foo"
"Foo\n".gsub("\n", '') # => "Foo"
"Foo\n".gsub('\n', '') # => "Foo\n"
'Foo\n'.gsub("\n", '') # => "Foo\\n"
'Foo\n'.gsub('\n', '') # => "Foo"
# pastie here: http://pastie.org/3924412
@wnuqui
wnuqui / gist:2717341
Created May 17, 2012 08:15
ruby's "system" call suddenly contradicts what I saw running the "rake tasks" in shell
# try running a rake task like
RAILS_ENV=test ANOTHER=imaginary rake foo:bar
# and run
rake foo:bar RAILS_ENV=test ANOTHER=imaginary
# both run right?
# now do this
ruby -e "system 'rake foo:bar RAILS_ENV=test ANOTHER=imaginary'"
@wnuqui
wnuqui / gist:2767143
Created May 22, 2012 06:45
module inside a class! what do you think?
class Foo
module Bar
def self.test
puts "Test!"
end
end
end
# Bar namespaced to Foo for the "test" util method!
# what do you think?
@wnuqui
wnuqui / gist:2822263
Created May 29, 2012 03:05
a friendly Ruby Object!
class Foo
def to_s
"I am friendly. You can debug here thru formatted string."
end
end
foo = Foo.new # "I am friendly. You can debug here thru formatted string."
foo # "I am friendly. You can debug here thru formatted string."
# alternatively
STDOUT << URI.escape(create(model.to_sym, options).to_json)