Instantly share code, notes, and snippets.

# Wilfrido Nuqui Jr.wnuqui

• Sort options
Created May 10, 2012
method chain
View gist:2651356
 class A def b data = [1, 2, 3] def data.foo! self.sample end data end end
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
Created May 12, 2012
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"
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)
Created May 17, 2012
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
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
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'"
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?
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
Created Jun 20, 2012
STDOUT!
View gist:2960811
 STDOUT << URI.escape(create(model.to_sym, options).to_json)
You can’t perform that action at this time.