Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChen Wang
7,371 PointsDoes ruby have a help method?
I'm new to ruby, and it seems like a really flexible language.
I have watched some ruby videos, and they just keeps invoking some method I have never seen.
I have java and python programming experiences. Therefore, I wonder if there is some helper method in ruby. Just like in python, you have
dir() # show what method can be invoked
help() # show documents of a specific method.
Or in java , you can simply see those things in some IDE like Eclipse.
Thanks.
2 Answers
John Steer-Fowler
Courses Plus Student 11,734 PointsYep, no problem. I see what you mean now.
You run the .methods method on the array to see which methods can be called.
Examples are better:
colors = ["Blue", "Green", "Red"]
colors.methods
=> [:inspect, :to_s, :to_a, :to_h, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :bsearch, :any?, :pack, :entries, :sort_by, :grep, :find, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :lazy, :nil?, :===, :=~, :!~, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
Hope this helps
Chen Wang
7,371 PointsYeah, this is exactly what I'm looking for.
Thanks John!!
John Steer-Fowler
Courses Plus Student 11,734 PointsYou're welcome. I hope you get on well with Ruby, personally it's my favourite language.
Keep up the good work
John Steer-Fowler
Courses Plus Student 11,734 PointsHey Chen,
Yep, Ruby has this.
In your console, type 'ri' which will open Ruby Interative.
You can then enter methods such as .length or .max and it will give you some documentation on that method.
Use Ctrl + z to quit Ruby Interactive.
Hope this helps
Example
$ ri
$ .length
------------------------------------------------------------------------------
length()
------------------------------------------------------------------------------
Returns the size of the collection calling size on the target. If the
collection has been already loaded, length and size are equivalent. If not and
you are going to need the records anyway this method will take one less query.
Otherwise size is more efficient.
class Person < ActiveRecord::Base
has_many :pets
end
person.pets.length # => 3
# executes something like SELECT "pets".* FROM "pets" WHERE "pets"."person_id" = 1
# Because the collection is loaded, you can
:
Chen Wang
7,371 PointsIt helps, thank you John.
Besides that, is it possible for me to know what are the potential methods of a object?
For example, if I have an array, I want to add an new element to it. It's really hard to remember so many different method names. So in Java, if you type a dot after a array, it can show you what methods can be called. Similarly, python has dir() for that. Is there anything I can do with Ruby?
Anthony Lucio
20,431 PointsAnthony Lucio
20,431 Pointstry ri within ruby.
here's a link if you need more info. (http://stackoverflow.com/questions/2009730/ruby-equivalent-to-pythons-help)
"It's definitely a poor cousin to ipython's help (and one of the main features I miss after moving to ruby), but you can also use ri from within irb. I'd recommend the wirble gem as an easy way to set this up."