-10

To solve various syntax problems like :

print "What's your fist name?"
first_name = gets.chomp 
put "Your name is #{first_name}!"
undefined method `put' for #<Context:0xbac070>

And could you share some good tricks in using the helper?

Sub-question: and any differences between .upcase and .capitalize?

marsrover
  • 424
  • 5
  • 16
  • 4
    It's really a 15-second google search. I mean, if you really hate just reading the documentation. You could also literally ASK RUBY `puts "Hello World!".methods` – toniedzwiedz Jun 22 '13 at 18:54
  • @Tom Agree with using `Object#methods` (as noted in my answer), but I always pair that with `sort`; for me, seeing things in order always help me find stuff more easily. – C. K. Young Jun 22 '13 at 18:56
  • 1
    @Tom 15 seconds but only if you type really slow. – JJJ Jun 22 '13 at 18:56
  • 1
    @Juhana That only applies if your browser has a search bar. Otherwise you have to clear out the current address (or open a new tab), type in `http://www.google.com/`, wait for that to load, click on the search box, _then_ type in your query. Phew! :-P – C. K. Young Jun 22 '13 at 18:59
  • No more down votes but more deletion, thanks! Being mean doesn't do us any good, everyone. And I am proud to just learn Ruby, so what? I had the right to learn it. Or the economy will be worse. – marsrover Aug 02 '13 at 03:15
  • It is insane that one couldn't delete his own post. And it makes me feel bad. – marsrover Aug 02 '13 at 03:19
  • That's a great idea that a noob uses puts "Hello World!".methods @Tom – marsrover Aug 04 '13 at 15:23
  • And I don't Google just because I trust the community here. @Tom, thank you for making noob disappointed. – marsrover Aug 04 '13 at 15:24
  • Maybe one should Google all his life and stop checking this site next time. @Tom – marsrover Aug 04 '13 at 15:26

2 Answers2

4

No, it's String#downcase. Use it like this:

lower = "Hello World!".downcase # => "hello world!"

Another approach to check about any particular method of a class as below:

"Hello World!".methods.grep(/down/) # => [:downcase, :downcase!]
"Hello World!".methods.grep(/lower/) # => []

any difference between .upcase and .capitalize?

Yes there is. Like below :

"hello".capitalize # => "Hello"
"hello".upcase # => "HELLO"

String#capitalize :- Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.

String#upcase :- Returns a copy of str with all lowercase letters replaced with their uppercase counterparts.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

OMG's answer is technically correct, but you should get in the habit of inspecting objects for which methods they support:

irb(main):001:0> "Hello, world!".methods.sort
=> [:!, :!=, :!~, :%, :*, :+, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :[], :[]=, :__id__, :__send__, :ascii_only?, :between?, :bytes, :bytesize, :byteslice, :capitalize, :capitalize!, :casecmp, :center, :chars, :chomp, :chomp!, :chop, :chop!, :chr, :class, :clear, :clone, :codepoints, :concat, :count, :crypt, :define_singleton_method, :delete, :delete!, :display, :downcase, :downcase!, :dump, :dup, :each_byte, :each_char, :each_codepoint, :each_line, :empty?, :encode, :encode!, :encoding, :end_with?, :enum_for, :eql?, :equal?, :extend, :force_encoding, :freeze, :frozen?, :getbyte, :gsub, :gsub!, :hash, :hex, :include?, :index, :initialize_clone, :initialize_dup, :insert, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :intern, :is_a?, :kind_of?, :length, :lines, :ljust, :lstrip, :lstrip!, :match, :method, :methods, :next, :next!, :nil?, :object_id, :oct, :ord, :partition, :prepend, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :replace, :respond_to?, :respond_to_missing?, :reverse, :reverse!, :rindex, :rjust, :rpartition, :rstrip, :rstrip!, :scan, :send, :setbyte, :singleton_class, :singleton_methods, :size, :slice, :slice!, :split, :squeeze, :squeeze!, :start_with?, :strip, :strip!, :sub, :sub!, :succ, :succ!, :sum, :swapcase, :swapcase!, :taint, :tainted?, :tap, :to_c, :to_enum, :to_f, :to_i, :to_r, :to_s, :to_str, :to_sym, :tr, :tr!, :tr_s, :tr_s!, :trust, :unpack, :untaint, :untrust, :untrusted?, :upcase, :upcase!, :upto, :valid_encoding?]

That will help you explore which methods are available to you.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • I did show him.. How to catch the fish also.. :) check my answer.. – Arup Rakshit Jun 22 '13 at 18:58
  • 1
    Yes, you edited your answer after I posted mine, but my point is still valid. ;-) – C. K. Young Jun 22 '13 at 19:00
  • Humm.. I answered step-by-step.. I am not a Robot within 1 min I will write everything... :) My answer has been posted within 1 min time difference... And I used `#grep` to show him how to eat the fish.. Hope that's not present in your one.. :) – Arup Rakshit Jun 22 '13 at 19:02