any tips?

0 views
Skip to first unread message

Marconi

unread,
Nov 15, 2009, 10:33:38 AM11/15/09
to Philippine Ruby Users Group (PRUG/PhRUG)
Hello guys, I'm new to Ruby and this past days I've been playing with
it but then I'm sometimes confused. When you're reading somebody's
code, how can you tell whether its a variable or a function call?

Since Ruby allows to omit parenthesis on function call this makes it
confusing to me. Any tips on how to deal with it?

Thanks.

Kates Gasis

unread,
Nov 15, 2009, 9:38:18 PM11/15/09
to ruby...@googlegroups.com
if you have not instantiated a local scope object i would assume it's
a method. remember, ruby and rails are heavily based on convention. if
you are coming from java you will need to unlearn some things.

def foo
puts bar # probably a method call
puts self.bar # most likely a method call
bar = "bash"
puts bar # a local object.
end



kates

Richard Gonzales

unread,
Nov 15, 2009, 9:42:00 PM11/15/09
to ruby...@googlegroups.com
Marconi,

There is a long list of functions (built-in), and I have to be frank that I haven't memorized
or plan to memorize them all. I deal with them as the necessity arise.

Mostly, if your using a lot of plugins, then a lot of helpers come in.
So a good way to spot function calls is being familiar with the helpers.

This link could help you out:

But please be aware that NOT ALL conform to this coding standards.

If your on Mac and using TextMate, CMD+SHIFT+F will help you locate where
the function was defined.

On Sun, Nov 15, 2009 at 11:33 PM, Marconi <cake...@gmail.com> wrote:

miguel manese

unread,
Nov 16, 2009, 1:47:13 AM11/16/09
to ruby...@googlegroups.com
Hi Marconi,

The "trick" is a change in perspective. In ruby, everything is a
function. Getters & setters, though they look like object properties,
are methods.

class Foo
attr_reader :read_only # this is like: def read_only; @read_only; end
attr_writer :write_only # this is like: def write_only=(x);
@write_only=x; end
attr_accessor :read_write # this is like def read_write;
@read_write; end; def read_write=(x); @read_write=x; end

def wombat
'This is as good as a "constant" property.'
end
end

Setters are kind of special syntactically in that you can do

foo.read_write = 'hello world' # foo.read_write=('hello world')

A more abstract perspective is by thinking that you are "sending a
message" instead of "calling a function"

foo.read_only # send message named 'read_only' to foo
foo.read_write = 1 # send message named 'read_write=' to foo, with parameter 1


- Jon

On Sun, Nov 15, 2009 at 11:33 PM, Marconi <cake...@gmail.com> wrote:
>

Marconi

unread,
Nov 16, 2009, 5:52:43 AM11/16/09
to Philippine Ruby Users Group (PRUG/PhRUG)
I'm actually starting to think this way. Thanks everyone.

Kates Gasis isinulat:

Richard Gonzales

unread,
Nov 16, 2009, 7:01:17 AM11/16/09
to ruby...@googlegroups.com
We are here to help and give back to the community what we learned.
Your queries are always welcome!
Reply all
Reply to author
Forward
0 new messages