Skip to content

Instantly share code, notes, and snippets.

Wilfrido Nuqui Jr. wnuqui

View GitHub Profile
View gist:2651356
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
Put your constant at the bottom
View gist:2652404
class A
def self.constant
[1, 2, 3]
end
B = constant and nil
end
@wnuqui
wnuqui / gist:2666644
Created May 12, 2012
safe conversions instead of to_x
View gist:2666644
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
What is your quick guess for the 2nd line?
View gist:2666669
# 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
prompt in rake task definition
View gist:2715883
# _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
where ruby's str#gsub can really prove that assumptions can really go wrong (any light?)
View gist:2717282
# 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
ruby's "system" call suddenly contradicts what I saw running the "rake tasks" in shell
View gist:2717341
# 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
module inside a class! what do you think?
View gist:2767143
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
a friendly Ruby Object!
View gist:2822263
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
View gist:2960811
STDOUT << URI.escape(create(model.to_sym, options).to_json)
You can’t perform that action at this time.