Very Basic Question

Discussion in 'Ruby' started by Gilman Gunn, Dec 7, 2008.

  1. Gilman Gunn

    Gilman Gunn Guest

    Hi all,

    I have a very basic question... what does "=>" mean? Two books I have
    looked at started using it without explaining and it is filtered in most
    searches.

    My guess is that it has something to do with assignment but am not sure.

    If anyone could shed some light on this it would be much appreciated.

    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
     
    Gilman Gunn, Dec 7, 2008
    #1
    1. Advertising

  2. 2008/12/7 Gilman Gunn <>:
    > I have a very basic question... what does "=>" mean?


    It's how you assign values to keys in a hash.

    letter_values = { 'a' => 1, 'b' => 2, 'c' => 3,..., z => '26' }
    letter_values['b'] # 2
    letter_values['j'] # 10

    Farrel
    --
    Aimred - Ruby Development and Consulting
    http://www.aimred.com
     
    Farrel Lifson, Dec 7, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    Using => in method calls used to puzzle me. I encountered this for the first
    time in FXRuby, and then in Ruby on Rails.

    It turned out that Ruby allows you to give a hash when invoking a method,
    where arguments to the method get rolled up into one hash variable. Consider
    this example (stolen from the Ruby Programming Wikibook[0]):

    def accept_hash(var)
    print "got: ", var.inspect
    end
    accept_hash :arg1 => 'giving arg1', :argN => 'giving argN'

    Hope you find this helpful.

    Regards,
    Yaser Sulaiman

    [0] http://en.wikibooks.org/wiki/Ruby_Programming

    P.S. In the 1st edition of Programming Ruby, it is mentioned that keyword
    arguments "are scheduled to be implemented in Ruby 1.8". Is this it (i.e.
    the keyword arguments feature), or is it something else?

    On Sun, Dec 7, 2008 at 11:50 AM, Chad Perrin <> wrote:

    > On Sun, Dec 07, 2008 at 04:49:46PM +0900, Gilman Gunn wrote:
    > > Hi all,
    > >
    > > I have a very basic question... what does "=>" mean? Two books I have
    > > looked at started using it without explaining and it is filtered in most
    > > searches.
    > >
    > > My guess is that it has something to do with assignment but am not sure.
    > >
    > > If anyone could shed some light on this it would be much appreciated.

    >
    > In addition to being used to assign values in hashes, it is also how the
    > interactive Ruby interpreter (called "irb") shows the return value for an
    > expression. For instance, at my tcsh console one might see this:
    >
    > ~> irb
    > irb(main):001:0> foo = 'first'
    > => "first"
    > irb(main):002:0> bar = 'second'
    > => "second"
    > irb(main):003:0> print foo, ' and ', bar, "\n"
    > first and second
    > => nil
    >
    > These return value indicators are useful for figuring out exactly what a
    > given expression does when writing code -- you may have an editor open in
    > which you are writing a program and, in another terminal emulator window,
    > have irb running so you can check how various expressions are evaluated
    > so you don't have to guess or constantly search through documentation to
    > be sure you're using expressions correctly.
    >
    > The `=>` in this usage is not actually part of the code, though the `=>`
    > in hash assignments *is* part of the code (of course).
    >
    > I hope that helps. Other than those two uses of the `=>` character
    > sequence, nothing springs immediately to mind.
    >
    > --
    > Chad Perrin [ content licensed OWL: http://owl.apotheon.org ]
    > Quoth Bjarne Stroustrup: "An ugly operation should have an ugly
    > syntactic form."
    >
     
    Yaser Sulaiman, Dec 7, 2008
    #3
  4. Gilman Gunn

    Phlip Guest

    Yaser Sulaiman wrote:

    > def accept_hash(var)
    > print "got: ", var.inspect
    > end
    > accept_hash :arg1 => 'giving arg1', :argN => 'giving argN'


    Better, Ruby 1.9 will let us fold the symbol and => notation:

    arg1: 'giving arg1', argN: 'giving argN'

    This means that Ruby has invented the "named argument" system, but it did it the
    right way; by building the feature out of low-level syntax elements that we can
    reuse for other situations. Ruby did not do what some languages do - invent
    named arguments using their own magic system that only works in method argument
    lists.

    --
    Phlip
     
    Phlip, Dec 7, 2008
    #4
  5. Gilman Gunn

    Gilman Gunn Guest

    Thx so much guys... I finally have my app up and running. No doubt more
    questions will follow!
    --
    Posted via http://www.ruby-forum.com/.
     
    Gilman Gunn, Dec 7, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    481
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    346
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,628
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    516
    Dave Thompson
    Feb 14, 2005
  5. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,235
    August Karlstrom
    Sep 26, 2008
Loading...

Share This Page