# Emergency help is needed

**URL:** <https://www.ruby-forum.com/t/emergency-help-is-needed/104619>\
**Category:** Ruby\
**Created:** [May 20, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619 "2007-05-20T20:27:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 20, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/1 "2007-05-20T20:27:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 20, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/2 "2007-05-20T20:49:07Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
anoosh writes:

> 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

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 20, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/3 "2007-05-20T21:58:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 20, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/4 "2007-05-20T22:00:18Z")

</div>

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/language.html](http://www.rubycentral.com/book/language.html)

For a definition of “instance variable” see

> **[Instance variable](https://en.wikipedia.org/wiki/Instance_variable)**
>
> In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e., a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similarities with a class variable, but is non-static. An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the cons An...

> **[Object-oriented programming](https://en.wikipedia.org/wiki/Object_oriented)**
>
> Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or properties), and the code is in the form of procedures (often known as methods). 
> A common feature of objects is that procedures (or methods) are attached to them and can access and modify the object's data fields. In this brand of OOP, there is usually a special name such as this or self used to refer to...

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](http://www.rubycentral.com/book/tut_exceptions.html)

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 21, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/5 "2007-05-21T19:06:35Z")

</div>

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.

1. I hope you mean “how do I handle exceptions in Ruby?” The Ruby  
equivalent of Java’s

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

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 21, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/6 "2007-05-21T19:30:39Z")

</div>

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

> 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

[\*] [Java SE Specifications](http://java.sun.com/docs/books/jvms/second_edition/html/)  
ConstantPool.doc.html

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 21, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/7 "2007-05-21T19:50:42Z")

</div>

On 5/20/07, anoosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![anoosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@anoosh](https://www.ruby-forum.com/u/anoosh)\
**Post date:** [May 21, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/emergency-help-is-needed/104619/8 "2007-05-21T22:21:06Z")

</div>

On May 21, 10:48 am, “Gregory B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Why does this sound like a homework assignment?

1. 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!
