Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Perl is more intuitive

by Joost (Canon)
on Aug 19, 2005 at 11:13 UTC ( [id://485067]=note: print w/replies, xml ) Need Help??


in reply to Perl is more intuitive

The ruby code you've written doesn't work for generic hashes (it does for arrays but it's not at all idiomatic).

You can write the ruby code for hashes a lot more intuitively like so:

#!/usr/bin/ruby # example data hash = { 'a' => '1', 'b' => '2', 'c' => '3', } hash.each do | k, v | puts "#{k} - #{v}<br/>" end

Update for the interested: ruby does have sigils, but they denote "scope" instead of "type": $ - global, @ - instance property and @@ - "static" class property. No sigil basically means lexical scope (automatic variable).

The reasoning behind this probably is, that variables that can be declared "somewhere else" in the code have sigils, but "local" variables don't need sigils since you can see that they are variables because they're declared a close by. In some respects ruby is even more fanatic about saving characters than perl :-) That there is no way to distinguish "type" by sigil is a good thing - since it makes it a lot easier to emulate the built-in types' API with user-defined classes.

Replies are listed 'Best First'.
Re^2: Perl is more intuitive
by kiat (Vicar) on Aug 19, 2005 at 11:44 UTC
    That looks very clean. And I supposed the each is a good-enough contextual clue that a hash is being called.
      each is actually a method name that's used for many of the container types; arrays use it too:

      arr.each { | x | # iterates the elements, NOT the index puts x }

      Basically, each here is just a method that recieves a block (like perl's anonymous subroutines) as an argument. The container type is then responsible for calling the block for each element.

      Any class can provide it's own each method - this is not a built-in function like perl's foreach.

        Ah, I see.

        Then if I'm new to the language and I'm reading someone's code, it may be harder for me to understand what's going on, unless the variables are named such as to give a hint e.g. arr_grades, hash_address.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://485067]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found