13

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!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96
  • 3
    Fifty? What is this, the final Rolling Stone issue of the century? – Ben Zotto May 28 '10 at 18:20
  • So just call it "important things to know" or something. What's the point of predicting the number of responses you get in the title of your question as if it was actually relevant to what you're asking? – Jimmy May 28 '10 at 18:27
  • 5
    It's made in Japan for a change! – Secko May 28 '10 at 18:27
  • Answers to this question would need a book. There actually is one, doing great job explaining "what you need to know about Ruby". I read it, and I love it. http://www.manning.com/black/ – Vojto May 28 '10 at 20:33
  • I want to improve in `Ruby` and `Rails`. A good idea is taking a quiz and understanding what you should learn. – Fabrizio Bertoglio Oct 27 '17 at 07:56

18 Answers18

12

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
Zach Langley
  • 6,776
  • 1
  • 26
  • 25
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 1
    This is mentioned in http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about – Andrew Grimm May 30 '10 at 03:36
11

Everything is an object. Everything.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 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 May 28 '10 at 18:26
  • The number 1984 is an object. When he said everything? ... he meant **everything**. – ANeves May 28 '10 at 18:35
  • 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 May 28 '10 at 18:37
  • 1
    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 May 28 '10 at 18:42
  • Blocks are not objects, kind of. http://www.skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/ – Jonas Elfström May 28 '10 at 18:43
  • It's appearing that everything is not an object. What is an object exactly (as far as Ruby is concerned)? – Brian T Hannan May 28 '10 at 18:59
  • 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 May 28 '10 at 19:06
  • 4
    @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. – Alex Wayne May 28 '10 at 19:18
  • 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 May 28 '10 at 19:59
8

Methods implicitly return the result of the last statement.

def foo
  "bar"
end

puts foo # outputs "bar"
Jimmy
  • 35,686
  • 13
  • 80
  • 98
  • 2
    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 May 30 '10 at 03:39
7
  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
  • 3,676
  • 19
  • 19
7

Strings are mutable; Symbols are not.

x1a4
  • 19,417
  • 5
  • 40
  • 40
5

Ruby uses message passing, not function calls.

e.g.,

# These are all the same:
5 + 3
5.+(3)
5.send(:+, 3)
Zach Langley
  • 6,776
  • 1
  • 26
  • 25
  • Nitpicking, but not quite, since you could override `Fixnum#send` and the third line could have a different effect. – Marc-André Lafortune May 28 '10 at 18:46
  • 1
    Many, many things about ruby could be false if you override core methods like `send` – Alex Wayne May 28 '10 at 19:21
  • 2
    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 May 28 '10 at 19:37
4

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
  • 259
  • 3
  • 5
3

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
  • 234,037
  • 30
  • 302
  • 389
  • 2
    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 May 28 '10 at 21:38
  • Hrm, that's a little surprising. I thought MRI was meant to be the reference implementation. Thanks for the info. – Chuck May 28 '10 at 21:45
  • Any code which depends on the return value of a class definition should be nuked form orbit. – Alex Wayne May 28 '10 at 22:52
  • 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 May 29 '10 at 00:37
2

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
  • 6,776
  • 1
  • 26
  • 25
  • 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 May 28 '10 at 18:58
  • 1
    Yes, classes may override `method_missing` to suit their needs. – mipadi May 28 '10 at 19:07
  • 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 May 28 '10 at 20:03
2

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
  • 14,781
  • 29
  • 95
  • 145
  • 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 May 29 '10 at 07:42
  • It's not too hard to write code that runs on both 1.8 and 1.9. – Andrew Grimm Jun 01 '10 at 02:11
  • @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 Jun 01 '10 at 13:31
  • True, code should be backwards compatible within all minor version changes ... but I suppose not necessarily major version changes. – Brian T Hannan Jun 01 '10 at 14:58
1

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
  • 32,417
  • 7
  • 53
  • 72
  • 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 May 28 '10 at 18:46
  • 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 May 28 '10 at 18:51
  • 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 May 28 '10 at 18:53
  • Can't seem to find anything on lp or ap – Brian T Hannan May 28 '10 at 18:54
1

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

See also Monkey-patching Vs. S.O.L.I.D. principles?

Community
  • 1
  • 1
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
1

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

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
1

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
  • 11,507
  • 7
  • 37
  • 59
0

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

P Shved
  • 96,026
  • 17
  • 121
  • 165
0

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
  • 106,965
  • 23
  • 235
  • 261
-2

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
  • 1,072
  • 1
  • 8
  • 13
  • 1
    This is a negative comment and doesn't give any insight to the question being asked. – Brian T Hannan May 29 '10 at 07:41
  • 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 May 29 '10 at 10:11
-11

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

vicatcu
  • 5,407
  • 7
  • 41
  • 65