How do I use define_method to create class methods?
I think in Ruby 1.9 you can do this: class A define_singleton_method :loudly do |message| puts message.upcase end end A.loudly “my message” # >> MY MESSAGE
I think in Ruby 1.9 you can do this: class A define_singleton_method :loudly do |message| puts message.upcase end end A.loudly “my message” # >> MY MESSAGE
As the author of Trollop, I cannot BELIEVE the stuff that people think is reasonable in an option parser. Seriously. It boggles the mind. Why should I have to make a module that extends some other module to parse options? Why should I have to subclass anything? Why should I have to subscribe to some … Read more
You can compare hashes directly for equality: hash1 = {‘a’ => 1, ‘b’ => 2} hash2 = {‘a’ => 1, ‘b’ => 2} hash3 = {‘a’ => 1, ‘b’ => 2, ‘c’ => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a … Read more
The comments referring to blocks and Procs are correct in that they are more usual in Ruby. But you can pass a method if you want. You call method to get the method and .call to call it: def weightedknn( data, vec1, k = 5, weightf = method(:gaussian) ) … weight = weightf.call( dist ) … Read more
I found the answer on SASS issues: https://github.com/sass/sass/issues/1768 Since OSX el Capitan there is a new security function that prevents you from modifying system files called Rootless. So you have 2 options: If you install gems on /usr/local/bin there will be no problem because rootless doesn’t affect this path. sudo gem install -n /usr/local/bin GEM_NAME_HERE
What about the following snippet? require ‘json’ value=”{“val”:”test”,”val1″:”test1″,”val2″:”test2″}” puts JSON.parse(value) # => {“val”=>”test”,”val1″=>”test1″,”val2″=>”test2″}
Simplest way I can think of: # checking whether foo is a boolean !!foo == foo
.post{:class => (“gray” unless post.published?)}
Rubygems.org’s Guides is one of the best resources for writing your own gem. If you’re using Bundler in your app, you might want to look at Ryan Bigg’s guide to Developing a RubyGem using Bundler and the Railscast on creating gems with Bundler. If you’re interested in tools to help you write gems: Jeweler – … Read more
The rad way => let(:dummy_class) { Class.new { include ModuleToBeTested } } Alternatively you can extend the test class with your module: let(:dummy_class) { Class.new { extend ModuleToBeTested } } Using ‘let’ is better than using an instance variable to define the dummy class in the before(:each) When to use RSpec let()?