"soemthing".pluralize/at/from/... -> NoMethodError

D

David Krmpotic

Hello,


I've been wondering about this for some time now.. Why don't any of the
following methods work: string.at/from/first/last/humanize/pluralize ?
IRB returns:

NoMethodError: undefined method `pluralize' for "something":String

You can try it yourself (also by using web IRB from here:
http://tryruby.hobix.com/)

Those methods are mentioned in "Agile Development with Rails" for
example.. you can also see people using them on Google code search.

However, they are not mentioned in documentation:
http://ruby-doc.org/core/classes/String.html

This is funny. Also.. string.to_json and string.to_yaml are mentioned in
the agile book, but don't work either.

This example doesn't go through in IRB:

# For demo purposes, create a Ruby structure with two attributes
Rating = Struct.new:)name, :ratings)
rating = Rating.new("Rails" , [ 10, 10, 9.5, 10 ])
# and serialize an object of that structure two ways...
puts rating.to_json #=> ["Rails", [10, 10, 9.5, 10]]
puts rating.to_yaml #=> --- !ruby/struct:Rating
name: Rails
ratings:
- 10
- 10
- 9.5
- 10

Can you help me understand all this? Thank you!

David
 
D

dblack

Hi --

Hello,


I've been wondering about this for some time now.. Why don't any of the
following methods work: string.at/from/first/last/humanize/pluralize ?
IRB returns:

NoMethodError: undefined method `pluralize' for "something":String

You can try it yourself (also by using web IRB from here:
http://tryruby.hobix.com/)

Those methods are mentioned in "Agile Development with Rails" for
example.. you can also see people using them on Google code search.

However, they are not mentioned in documentation:
http://ruby-doc.org/core/classes/String.html

This is funny. Also.. string.to_json and string.to_yaml are mentioned in
the agile book, but don't work either.

Those methods are add-ons that come with Rails. They're not part of
standard Ruby.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Jamis Buck

David,

Those are not standard Ruby methods. They are defined in the Rails
ActiveSupport library, and thus can only be accessed from within Rails
applications (or other applications that explicitly require
ActiveSupport).

- Jamis

David said:
Hello,


I've been wondering about this for some time now.. Why don't any of the
following methods work: string.at/from/first/last/humanize/pluralize ?
IRB returns:

NoMethodError: undefined method `pluralize' for "something":String

You can try it yourself (also by using web IRB from here:
http://tryruby.hobix.com/)

Those methods are mentioned in "Agile Development with Rails" for
example.. you can also see people using them on Google code search.

However, they are not mentioned in documentation:
http://ruby-doc.org/core/classes/String.html

This is funny. Also.. string.to_json and string.to_yaml are mentioned in
the agile book, but don't work either.

This example doesn't go through in IRB:

# For demo purposes, create a Ruby structure with two attributes
Rating = Struct.new:)name, :ratings)
rating = Rating.new("Rails" , [ 10, 10, 9.5, 10 ])
# and serialize an object of that structure two ways...
puts rating.to_json #=> ["Rails", [10, 10, 9.5, 10]]
puts rating.to_yaml #=> --- !ruby/struct:Rating
name: Rails
ratings:
- 10
- 10
- 9.5
- 10

Can you help me understand all this? Thank you!

David
 
C

Carlos López

I wanted say:
"you must require 'rubygems' before 'active_support'" (s/after/before)
:p
 
D

David Krmpotic

Hmm, when in IRB, I enter:

irb(main)> require 'rubygems'
=> true
irb(main)> require_gem 'activesupport'
=> true

but I still get NoMethodError when doing "something".pluralize .. now
sure, I think it worked 2 days ago...

What am I missing?
thank you
 
D

David Krmpotic

ah now I know why.. this works:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support'
=> true

interesting.. can someone explain what is happening?
so require_gem is no good in irb .. why it doesn't report an error?


thank you
 
A

Austin Ziegler

ah now I know why.. this works:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support'
=> true

interesting.. can someone explain what is happening?
so require_gem is no good in irb .. why it doesn't report an error?

Yes. require_gem is deprecated. Don't use it if at all possible.

-austin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,411
Latest member
CarmaStarn
Top