I'm trying to understand how method invocation works in Ruby objects. Ruby docs list a bunch of methods to execute on ruby objects. When I try one of them
puts RUBY_VERSION
puts Time.new(2008,6,21, 13,30,0, "+09:00").utc.seconds_since_midnight
I get the following output
1.9.3
bin/musor.rb:14:in `<main>': undefined method `seconds_since_midnight' for 2008-06-21 13:30:00 +0900:Time (NoMethodError)
What's wrong with the call I make?
seconds_since_midnight is Time extension added by rails. If you want to use it, you will need to add require 'activesupport/core_ext' and install activesupport gem first.
Related
I'm doing a Ruby training on Codewars and I'm stuck on something.
I have to do this :
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
# returns 'Bart, Lisa & Maggie'
So I tried this code:
def list names
names.map(&:values).flatten.to_sentence(last_word_connector: ' &')
end
But unfortunately, I have this error :
main.rb:4:in `list': undefined method `to_sentence' for ["Bart", "Lisa", "Maggie", "Homer", "Marge"]:Array (NoMethodError)
Did you mean? to_set
from main.rb:6:in `<main>'
I've been searching for hours and I don't understand why 'to_sentence' doesn't work...
Thanks.
Array#to_sentence is an ActiveSupport method. ActiveSupport is a Ruby gem that adds a lot of useful utility methods to existing classes. You'll need to install and require the gem to make sure it's loaded
gem install activesupport
Then, at the top of your file,
require 'active_support/all'
If you're writing a Rails project, then you get ActiveSupport for free. But if you're writing standard Ruby, you have to require it.
I have the following:
input = gets.chomp
basket = input.strip.split(',')
basket.delete_if(&:blank?)
which should allow you to do: cat, dog,,
from there we can split that up, strip it clean and split it on , and then check for empty elements and remove them.
but this code gives me an error: delete_if: undefined method 'blank?' for "cat":String (NoMethodError) which does not make any sense to me. I thought that the whole purpose of blank? or empty? was to say remove this element if this is true.
ActiveSupport that comes with Rails adds the blank? method to String and many other classes. Since this method is not part of Ruby core, you need to have Rails or the ActiveSupport gem installed. If that gem is installed than you can require ActiveSupport's core extensions like this:
> 'foo'.blank?
# => NoMethodError: undefined method `blank?' for "foo":String
> require 'active_support/core_ext'
# => true
> 'foo'.blank?
# => false
require 'active_support'
require 'active_support/core_ext'
import "active_support" first
blank? is a method augmented to the String class from rails. It's not part of the ruby String class by default.
If I require ap, irb returns true (I assume telling me that the awesomeprint gem has been successfully loaded). However if I issue the command ap f where f is a hash, I get:
NoMethodError: undefined method `ap' for main:Object
from (irb):5
from /usr/local/bin/irb:12:in `<main>'
Thoughts?
Here is my $LOAD_PATH:
"/usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.1.0/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/multi_xml-0.4.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/httparty-0.8.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/ap-0.1.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/psych-1.2.2/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/crack-0.3.1/lib", "/usr/local/lib/ruby/site_ruby/1.9.1", "/usr/local/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.3.0", "/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/vendor_ruby/1.9.1", "/usr/local/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.3.0", "/usr/local/lib/ruby/vendor_ruby", "/usr/local/lib/ruby/1.9.1", "/usr/local/lib/ruby/1.9.1/x86_64-darwin11.3.0"
It seems ap is the 4th one.
The gem you want is called awesome_print, so sudo gem install awesome_print should fix it. May want to remove the other gem 'ap' which seems to be some http and xml related gem.
I've just installed RubyMine, as long as Ruby + gem + rspec + rspec-rails.
I'm trying to make run the mini-tutorial shown on http://rspec.info/ but I'm having problems.
First, I had to modify the shown
require 'bowling'
to
require_relative 'bowling'
as I was getting a
`require': no such file to load -- bowling.
Now, even after doing this I am getting a
`<top (required)>': undefined method `describe' for main:Object (NoMethodError)
How to make rspec work in RubyMine?
rspec.info is for RSpec version 1.x. Documentation for 2.x, which is what you will have installed if you simply did gem install rspec can be found at http://relishapp.com/rspec. You may want to take a look at their tutorial there.
I use RSpec and RubyMine together every day, so I expect that version mismatch is your biggest problem.
Also, if you are willing to purchase a book to learn RSpec, I'd highly recommend http://pragprog.com/book/achbd/the-rspec-book from Pragmatic Programmers.
Well, it seems to be a known issue:
http://youtrack.jetbrains.net/issue/RUBY-8603?projectKey=RUBY
EDIT: it did indeed seem to work.
The following yields an error:
require 'test/unit'
Test::Unit.setup_argv(["tests"])
$ run_tests.rb:4: undefined method `setup_argv' for Test::Unit:Module (NoMethodError)
How can I make Ruby use the Test::Unit class instead of the Test::Unit module for the method call?
EDIT Ruby 1.8.7
The reason for the error is that setup_argv is not available in Ruby 1.8.7.
Test::Unit is always a module. There is no class.
See the 1.8.7 docs here for how to use:
http://apidock.com/ruby/v1_8_7_330/Test/Unit