Emergency help is needed

A

anoosh

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?
 
D

Dan Zwell

anoosh said:
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
 
B

Brian Candler

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
http://en.wikipedia.org/wiki/Instance_variable
http://en.wikipedia.org/wiki/Object_oriented

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
 
M

Morton Goldberg

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
 
X

Xavier Noria

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_edition/html/
ConstantPool.doc.html
 
G

Gregory Brown

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?
 
B

bramski

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top