views:

600

answers:

18

I am new to the language and I need to know what are the top things that are absolutely necessary to know in order to make a fully functional website or web app using the Ruby programming language?

Mainly Ruby on Rails with Rake and other tools that mainly use Rake.

Update: I know many other languages like C++, Java, PHP, Perl, etc, etc ....

Update 2: This is great ... keep 'em coming!

A: 

nobody would have ever heard of it if it wasn't for Rails?

vicatcu
You failed to give any insight about the real question at hand ... although your statement is probably true.
Brian T Hannan
It was used before rails, just not nearly as much.
ryeguy
I experimented with it several years before Rails came into existence.
Paul Nathan
I like Ruby a lot more than I like Rails...
Earlz
Ruby slowly supersedes Perl in my everyday scripting, even without any rails. (And please, don't delete the answer: you won't lose rep due to CW, and it is worth hanging here with a low score)
Pavel Shved
+7  A: 

Everything is an object. Everything.

Squeegy
Is there any way to make something not an object? Like in C/C++ you CAN do objects but you don't have to. I believe Java is the same way ... you can just have a main function that does all the work ... but you're probably going to have some objects. PHP can have objects but it is not required and many times objects in PHP are not used.
Brian T Hannan
The number 1984 is an object. When he said everything? ... he meant **everything**.
ANeves
Even the class of any object IS an object, and the class of class....and even nil is an object.. and 'true' and 'false' are too..and class(or object) _method_ can be an object too..
zed_0xff
Everything *appears* to be an object, most of the time. But some things aren't actually objects: Try `a = 2 ; def a.foo ; end` to see where the illusion breaks down.
Wayne Conrad
Blocks are not objects, kind of. http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/
Jonas Elfström
It's appearing that everything is not an object. What is an object exactly (as far as Ruby is concerned)?
Brian T Hannan
But it seems like just about everything is an object which is cool? Is the code that makes up an object called a class like in C++, C# and Java?
Brian T Hannan
@Brian T Hannan: An object is something with methods you can call and (optionally) and internal state. In `ObjectiveC` this is not an object `int i = 3`. You cannot do `[i toString]` because `i` is not an object that does not have methods that you can call. in Ruby `i = 3; i.to_s #=> "3"`. Even a simple integer is an object with methods you can call just like any other object. And you can write ruby in a non-object oriented style, without creating your own classes and such, but it doesn't change the fact everything you use in that code is actually an object.
Squeegy
Actually I remember reading something where something in Ruby was **not** an object. I believe it was a certain way of having methods or code blocks or something. Oh and integers of course only *appear* to be an object
Earlz
+9  A: 

Everything (except false and nil) evaluates to true in a boolean context.

This is different from other languages where empty constructs or 0 frequently evaluate as false.

if 0
    puts "0 evaluates to true"
end
ChristopheD
This is mentioned in http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about
Andrew Grimm
+1  A: 

irb, pp, lp & ap are very helpful magic words! ;)

  1. http://github.com/oggy/looksee = lp
  2. http://github.com/michaeldv/awesome_print = ap
zed_0xff
I'm not sure what those links have to do with anything. Can you elaborate on irb, pp, lp and ap? ... why are they magically? ... atleast just a brief discussion.
Brian T Hannan
I've heard of irb, it's a very nice feature. pp seems cool ... so I it's kind of like Java's ToString() method that can be overwritten for an object so that you can print out a pretty-viewing of the object if you want to do?
Brian T Hannan
irb is a "interactive ruby" console in which you can run your code immediately, and other tools are great tools for inspecting any object/class/module methods and/or inheritance. great help on debugging or learning some new APIs
zed_0xff
Can't seem to find anything on lp or ap
Brian T Hannan
+4  A: 

Ruby uses message passing, not function calls.

e.g.,

# These are all the same:
5 + 3
5.+(3)
5.send(:+, 3)
Zach Langley
Nitpicking, but not quite, since you could override `Fixnum#send` and the third line could have a different effect.
Marc-André Lafortune
Many, many things about ruby could be false if you override core methods like `send`
Squeegy
To nitpick more, there's `__send__` for the cases where `send` has been overridden. It happens. I would argue that the only reason for overriding `__send__`, however, is malice.
x1a4
+6  A: 
  1. You can use ruby modules as mixins where your design requires multiple inheritance
  2. Every method returns the value of its last statement as the value of the method though you can use return where you want to be more explicit.
  3. You can open any class again and add methods to it, called monkey patching which can be very powerful if used sensibly otherwise hell will break.
  4. You can pass a block of code to any method and operate on it, e.g. in ruby world most of the time coders use iterators instead of for loops.
  5. Where possible use symbols instead of strings because they are efficient, e.g. in hash keys

etc..

nas
+2  A: 

If a method Ruby doesn't know about is called, Ruby will then call method_missing with the details.

class MyClass
  def method_missing(method_name, *args)
    puts "Method #{method_name} called with arguments #{args.join(', ')}"
  end
end

my_instance = MyClass.new
my_instance.undefined_method(4, :blah)  # => Method undefined_method called with arguments 4, blah
Zach Langley
Interesting ... is this extendable? Is this a global function or something you can choose to provide in an class/object you create?
Brian T Hannan
Yes, classes may override `method_missing` to suit their needs.
mipadi
This is one of the important distinctions between message dispatch and method calls. When an object receives a message in Ruby, it will perform *some* method in response. If there is a method that matches the method, those one is chosen. Otherwise the object performs `method_missing`, which will allow it to respond to totally arbitrary methods.
Chuck
+5  A: 

Strings are mutable; Symbols are not.

x1a4
+1  A: 

Don't abuse monkey patching. Ruby makes it easy, but it can hurt.

See also http://stackoverflow.com/questions/293236/monkey-patching-vs-s-o-l-i-d-principles

Marco Mariani
+6  A: 

Methods implicitly return the result of the last statement.

def foo
  "bar"
end

puts foo # outputs "bar"
Jimmy Cuadra
As do blocks. Remember not to do any printf debugging after the (formerly) last line, otherwise you'll be returning a different value!
Andrew Grimm
A: 

You can't overload operator= just like you did in C++. You also can't simulate it in any way that makes sense.

Pavel Shved
You can do a conditional that asks what class the object is. What do you mean by the second sentence?
Andrew Grimm
@Andrew I mean anything that *looks* like `operator=` was overloaded.
Pavel Shved
+1  A: 

A worth full read:-

Top 5 New Features in Ruby 1.9

  1. YARV - Yet Another Ruby VM
  2. Fibers
  3. Named Regexp Groups

4.The Lambda Operator

5.RubyGems and Rake are Merged with Ruby

piemesons
+2  A: 

Almost everything is an expression that returns a value. This includes things you might not normally think of as expressions, such as class definitions — a class definition evaluates to the last expression inside of it. Likewise, if and case are expressions that evaluate to same result as the last expression in whichever branch was taken.

Exactly what they evaluate to if not always immediately obvious, though: def evaluates to nil, but define_method evaluates to a Proc representing the method it defined. This combined with the fact that a class definition is an expression sometimes surprises people:

class A
  define_method(:foo) {"hello"}
end

# => #<Proc:0x0001d718@(irb):18> # NOT nil

class A
  define_method(:bar) {"hello"}
  FAVORITE_NUMBER = 80
end

# => 80
Chuck
Actually, the return value of `def` is implementation-defined, not `nil`. In Rubinius, for example, it returns the `CompiledMethod` object corresponding to that method.
Jörg W Mittag
Hrm, that's a little surprising. I thought MRI was meant to be the reference implementation. Thanks for the info.
Chuck
Any code which depends on the return value of a class definition should be nuked form orbit.
Squeegy
Oh, absolutely. The point was just how pervasive expressions are. I see I didn't really explain the difference, though, so I'll revise it once I'm not on an iPhone.
Chuck
A: 

If you actually know how to program, you already know the most important things to know about the Ruby Programming language. Just follow them.

The rest is in the stack of manuals; read them.

TerryP
This is a negative comment and doesn't give any insight to the question being asked.
Brian T Hannan
Perhaps it can be read negatively but the same can be said of most terse messages. The statement about knowing how to program is however correct; and the OP claims enough linguistic experience that it shouldn't be taken offensively ;).
TerryP
A: 

ruby-debug is your friend. I probably use it more than any other gem. Given ruby's dynamic nature it's hard to know exactly what a particular piece of code does by looking at the text in your editor. Just throw a 'debugger' in front of it and step into it.

it's also a great way to find out where a dynamically generated method is coming from.

jshen
+3  A: 

Rack. All modern Ruby web servers and frameworks use the Rack protocol. Although you can make a Ruby web app without knowledge of Rack, it will give you a good foundation and it is becoming increasingly more important in the Ruby web community.

Rack may also make a good starting point for learning to program web apps in Ruby, since you can start with the simplest web app:

run lambda { |env| [200, {}, "hello world"] }

and you can keep building from there. After you understand Rack, the architectural decisions in Sinatra, Rails, etc. make more sense.

http://rack.rubyforge.org/

techiferous
+1  A: 

v 1.9 is very very different than v 1.8. Code built for 1.8 is NOT guaranteed to work in 1.9, and vice versa.

I had a whole bunch of code that worked in v 1.8 and didn't in v 1.9, and then had to deal with two machines that had different versions on each. Not fun.

Make sure to choose a version (probably the latest, but be wary that a lot of sample code in blogs on the web is 1.87, as is the second edition of Programming Ruby. There's since been released a third edition of Programming Ruby that covers 1.9, and that's the one you want.

Also, if you're at all like me, you'll be singing one of three songs while programming it:

mmr
I will definitely be singing Ruby Soho by Rancid now that you mention it ... and I'll be playing it loudly on my speakers!
Brian T Hannan
It's not too hard to write code that runs on both 1.8 and 1.9.
Andrew Grimm
@Andrew Grimm-- that may be, but 1.9 is not backwards compatible with 1.8. To me, as soon as you break backwards compatibility, that should at least be a major version change, not a minor one.
mmr
True, code should be backwards compatible within all minor version changes ... but I suppose not necessarily major version changes.
Brian T Hannan
A: 

I think people have covered most of the basics here. Here's some advanced stuff:

inherited, included, class_eval, instance_eval and:

@a = "something"
class << @a
  def moo
    puts "moo"
  end
end
@a.moo
@b = "something else"
@b.moo

eval is evil. Never use it unless you really, really, really know what you're doing.

Ryan Bigg
What does the code sample have to do with eval? I'm confused.
Brian T Hannan
It doesn't, it's just a side mention that you shouldn't be using it.
Ryan Bigg