Ali Koubeissi wrote:
> Hey, I've started with Ruby two days ago, and I have some questions.
>
> 1- What's the use of Symbols? I've read that symbols are (Symbols are
> lightweight strings. Usually, symbols are used in situations where you
> need a string but you won’t be printing it to the screen.), however I
> cannot understand how to use them, and so I tried to read some examples
> and I got this:
>
> kitty_toys =
> [:shape => 'sock', :fabric => 'cashmere'] +
> [:shape => 'mouse', :fabric => 'calico'] +
> [:shape => 'eggroll', :fabric => 'chenille']
> kitty_toys.sort_by { |toy| toy[:fabric] }
>
> Why is he using symbols? I just couldn't understand ;(.
>
> 2- Why does a block has to be on the same line when passing argument?
> Like in the previous example: kitty_toys.sort_by { |toy| toy[:fabric] }.
> If I tried to move the block down, it won't work. Also, if someone could
> explain how does the argument get passed to the block. I mean, based on
> what?
>
> 3- How does a Ruby file compile? Like in Java, your code gets compiled
> into bytecode, and then delivered to the JVM. Are there any similarities
> in Ruby?
>
> I'm currently reading Programming Ruby 2nd Edition, and this website:
> http://poignantguide.net/ruby/chapter-1.html . Any recommendations ?
>
> Thanks in advance.
Welcome to ruby. First to start the question about Symbols. Well, they
are used in hashes a lot, you could very well do { 'shape' => 'sock' }
and it works the same, but just to be consistent you use the object as a
symbol and it's value as what ever else. There is a lot more to that,
and i'm sure someone else will reply to tell you that.
2. A block doesn't have to be on the same line.
kitty_toys.sort_by do |toy|
toy[:fabric]
end
notice it's on 3 lines, but with this small of a block it looks ugly.
The argument gets passed to the block by means of the "slide" or the
pipes. Not sure if that was the answer you were looking for, but there
you go.
3. Ruby compiles on runtime by the ruby interpreter. I don't really know
a whole lot about how all that works.
Hope that clears something up for ya.
~Jeremy
--
Posted via
http://www.ruby-forum.com/.