Emergency help is needed

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?

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)

I can't think of any synonym in either for symbols, because both are
compiled languages. Instance variables are pretty much the same beast
as non-static Java class variables.

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

I have no idea what you mean. You mean instead of the standard
exception handling? I don't think you can, and I certainly hope you
don't try.

-s

···

In message <1179685177.363826.262700@u36g2000prd.googlegroups.com>, anoosh writes:

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

1)What are the meaning of symbols(:var) and Instance variables(@x)

http://www.rubycentral.com/book/language.html

For a definition of "instance variable" see

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_exceptions.html

···

On Mon, May 21, 2007 at 03:25:06AM +0900, anoosh wrote:

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

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

···

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?

Why does this sound like a homework assignment?

···

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?

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

[*] Java SE Specifications

···

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? :slight_smile:

1a. There is nothing like Ruby symbols in Java or C.

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!

···

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?