[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Emergency help is needed

paytam

5/20/2007 6:20:00 PM

Hi all
I have 2 questions?
1)What are the meaning of symbols(:var) and Instance variables(@x)
Ruby?(I know Java and C.If it is possible for you tell me the
synonyms in these languages,please)

2)How can I define my own exception handling in Ruby?

6 Answers

Dan Zwell

5/20/2007 7:58:00 PM

0

anoosh wrote:
> Hi all
> I have 2 questions?
> 1)What are the meaning of symbols(:var) and Instance variables(@x)
> Ruby?(I know Java and C.If it is possible for you tell me the
> synonyms in these languages,please)
>
> 2)How can I define my own exception handling in Ruby?
>
>

Symbols tend to be used like constants sometimes are in other languages.
Take this line of java:

jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);

If ruby had this class with these methods, it would probably use symbols
instead of constants:

jSplitPane.setOrientation(:vertical_split)

Symbols don't have scope--they are not variables. Symbols are unique,
and they do not contain a value the same way that variables do. I have
seen them compared to numbers--'4' is unique and its value cannot be
changed. Also like numbers, you do not need to create them--you can just
start referencing them.

In C, symbols can be used like (instead of) enums--for constants whose
value does not actually matter.

Dan

Brian Candler

5/20/2007 8:00:00 PM

0

On Mon, May 21, 2007 at 03:25:06AM +0900, anoosh wrote:
> 1)What are the meaning of symbols(:var) and Instance variables(@x)

http://www.rubycentral.com/book/lan...

For a definition of "instance variable" see
http://en.wikipedia.org/wiki/Instanc...
http://en.wikipedia.org/wiki/Objec...

I'm not sure what Java calls them - attributes? fields? properties? It's the
state maintained by an individual object. The thing you use Java setters and
getters to access.

> 2)How can I define my own exception handling in Ruby?

http://www.rubycentral.com/book/tut_excep...

Morton Goldberg

5/21/2007 5:06:00 PM

0

On May 20, 2007, at 2:25 PM, anoosh wrote:

> Hi all
> I have 2 questions?
> 1)What are the meaning of symbols(:var) and Instance variables(@x)
> Ruby?(I know Java and C.If it is possible for you tell me the
> synonyms in these languages,please)
>
> 2)How can I define my own exception handling in Ruby?

That's really three questions, but who's counting? :)

1a. There is nothing like Ruby symbols in Java or C. Perhaps the best
way for you to think about symbols is as objects that can serve as
unique identifiers. Their main advantage (over strings) is very quick
equality comparison. This makes them good hash keys. They are never
garbage collected, which means you probably shouldn't use them in a
situation where you are making a lot of objects for temporary use.

1b. Instance variables are pretty much equivalent to the field
members of a Java class.

2. I hope you mean "how do I handle exceptions in Ruby?" The Ruby
equivalent of Java's

try { ... } catch(<excpetion_name> e) { ... } finally { ... }

is

begin
...
rescue <excpetion_name> => e
...
ensure
...
end

You will really have to read up on this to use it effectively, but
knowing the keywords will allow you to Google for further help.

Regards, Morton

Xavier Noria

5/21/2007 5:28:00 PM

0

On May 21, 2007, at 7:05 PM, Morton Goldberg wrote:

> On May 20, 2007, at 2:25 PM, anoosh wrote:
>
>> Hi all
>> I have 2 questions?
>> 1)What are the meaning of symbols(:var) and Instance variables(@x)
>> Ruby?(I know Java and C.If it is possible for you tell me the
>> synonyms in these languages,please)
>>
>> 2)How can I define my own exception handling in Ruby?
>
> That's really three questions, but who's counting? :)
>
> 1a. There is nothing like Ruby symbols in Java or C.

The usage of symbols is very similar to Java strings though. In Java
strings are inmutable and the JVM stores them once[*], quite close.
I'd say Java strings are kind of synonym for Ruby symbols, and
StringBuffer the synonym for Ruby strings (seen just as data types,
of course APIs are different in both cases).

I think that they are not exact synonyms nonetheless, because the
semantics are not exactly the same. I mean, people use Ruby strings
where you'd use a string in Java. Symbols are often used for stuff
that is somehow "specific" (a :width option), while strings are used
to represent "data". In Java you don't have the choice.

-- fxn

[*] http://java.sun.com/docs/books/jvms/second_edi...
ConstantPool.doc.html

Gregory Brown

5/21/2007 5:49:00 PM

0

On 5/20/07, anoosh <paytam@gmail.com> wrote:
> Hi all
> I have 2 questions?
> 1)What are the meaning of symbols(:var) and Instance variables(@x)
> Ruby?(I know Java and C.If it is possible for you tell me the
> synonyms in these languages,please)
>
> 2)How can I define my own exception handling in Ruby?

Why does this sound like a homework assignment?

bramski@gmail.com

5/21/2007 8:15:00 PM

0

On May 21, 10:48 am, "Gregory Brown" <gregory.t.br...@gmail.com>
wrote:
> On 5/20/07, anoosh <pay...@gmail.com> wrote:
>
> > Hi all
> > I have 2 questions?
> > 1)What are the meaning of symbols(:var) and Instance variables(@x)
> > Ruby?(I know Java and C.If it is possible for you tell me the
> > synonyms in these languages,please)
>
> > 2)How can I define my own exception handling in Ruby?
>
> Why does this sound like a homework assignment?

2) If by "your own exception handling" you mean, how do I catch and
throw my own custom exceptions, here's a snippet.

class MyOwnException < StandardError
end

raise MyOwnException, "Hello World!"

this will raise a custom exception class that you can use to handle
your own separate types of exceptions. It's quite handy for
classifying your universe of exceptions for whatever class/api/handler/
cattle_prod you happen to be writing.

Cheers!