Forum: Ruby Learning Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jesse M (Guest)
on 2006-04-20 01:09
Okay I've went through Chris P.s tutorial on
programming(http://pine.fm/LearnToProgram/), and I still don't fully
understand what I hear from experienced Ruby programmers.  Is there any
other tutorials that you might spare for a newbly soul such as mine?
Jeppe J. (Guest)
on 2006-04-20 01:47
(Received via mailing list)
Try "programming ruby". The first edition of it can be accessed for
free,
although I recommend that you buy the second edition of it.

2006/4/19, Jesse M <removed_email_address@domain.invalid>:
Dave B. (Guest)
on 2006-04-20 02:08
(Received via mailing list)
Jesse M wrote:
> Okay I've went through Chris P.s tutorial on
> programming(http://pine.fm/LearnToProgram/), and I still don't fully
> understand what I hear from experienced Ruby programmers.  Is there any
> other tutorials that you might spare for a newbly soul such as mine?

What don't you understand?

Cheers,
Dave
Nathan O. (Guest)
on 2006-04-20 02:22
It can be hard to notice the nice little things until you actually
implement something. I'm just now knee deep in my first Ruby-based
project, and not until just recently have I seen what people are talking
about.

Basically, it's all just time-saving stuff that doesn't make ugly code.
You wont appreciate it until you write some code that should be hideous,
but turns out (almost) elegant.

Now that I've posted this, I'm off to post about something weird,
unpredictable and frustrating that I've found in Ruby :-)
James B. (Guest)
on 2006-04-20 02:51
(Received via mailing list)
Jesse M wrote:
> Okay I've went through Chris P.s tutorial on
> programming(http://pine.fm/LearnToProgram/), and I still don't fully
> understand what I hear from experienced Ruby programmers.  Is there any
> other tutorials that you might spare for a newbly soul such as mine?
>

http://ruby-doc.org/gettingstarted

http://ruby-doc.org/whyruby

--
James B.

?Design depends largely on constraints.?
  ? Charles Eames
James B. (Guest)
on 2006-04-20 02:54
(Received via mailing list)
Jesse M wrote:
> Okay I've went through Chris P.s tutorial on
> programming(http://pine.fm/LearnToProgram/), and I still don't fully
> understand what I hear from experienced Ruby programmers.  Is there any
> other tutorials that you might spare for a newbly soul such as mine?
>

http://ruby-doc.org/gettingstarted

http://ruby-doc.org/whyruby

--
James B.

?Design depends largely on constraints.?
  ? Charles Eames
Jesse M (Guest)
on 2006-04-20 05:26
Dave B. wrote:
> Jesse M wrote:
>> Okay I've went through Chris P.s tutorial on
>> programming(http://pine.fm/LearnToProgram/), and I still don't fully
>> understand what I hear from experienced Ruby programmers.  Is there any
>> other tutorials that you might spare for a newbly soul such as mine?
>
> What don't you understand?
>
> Cheers,
> Dave

Well I see something like,

lass Numeric
     def positive?
       self > 0
     end
   end

   [1, 2, 3].all? &:positive?  => true
   [-1, 2, 3].all? &:positive? => false

which i don't understand half of it :) and i'm really gung hoe about it!
超群李 (Guest)
on 2006-04-20 06:35
(Received via mailing list)
I don't know the syntax.But I think it is same as

[1,2,3].all? {|i| i.positive?}


On 4/20/06, Jesse M <removed_email_address@domain.invalid> wrote:
> > Cheers,
>    [1, 2, 3].all? &:positive?  => true
>    [-1, 2, 3].all? &:positive? => false
>
> which i don't understand half of it :) and i'm really gung hoe about it!
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


--
Chaoqun Li
ThoughtWorks(Xi'an)
mobile phone:13909217679
office phone:029-87607341
Dave B. (Guest)
on 2006-04-20 09:28
(Received via mailing list)
Jesse M wrote:
> Well I see something like,
>
> lass Numeric
>      def positive?
>        self > 0
>      end
>    end

I assume you understand this from Pine's LtP.

>    [1, 2, 3].all? &:positive?  => true
>    [-1, 2, 3].all? &:positive? => false
>
> which i don't understand half of it :) and i'm really gung hoe about it!

Have you tried typing this into IRB? I think you'll find it doesn't even
work.

The & symbol is used to get a Proc from a block passed into a method

  class Array
    def eachEven(&wasABlock_nowAProc)  # from LtP Online Chapter 10
  ...

or to convert a Proc into a block when _calling_ a method

  f = proc do |oddBall|
    puts oddBall.to_s+' is NOT an even number!'
  end
  [1, 2, 3, 4, 5].eachEven &f

:positive? is a symbol, so it can't be converted into a block:

all? and positive? are just methods (methods can end in !, =, or ?).

irb(main):001:0> [1,2,3].all? &:positive?
TypeError: wrong argument type Symbol (expected Proc)
        from (irb):1

Then, the "=> true" and "=> false" aren't actually Ruby code, they're
just meant to show the reader the value of that expression. Usually we
put a # in front so it's valid Ruby code:

  positive = proc {|n| n > 0 }
  [1, 2, 3].all? &positive   #=> true
  [-1, 2, 3].all? &positive  #=> false

Do you follow that (working) rewrite of the snippet? If you type it into
IRB, you should see the results listed after the "#=>".

Anyway, for further reading, I recommend Why's (Poignant) Guide to Ruby
if you can deal with reading a wacky story in between learning more
Ruby.

http://www.poignantguide.net/ruby/

Or there's the standard reference, the Pickaxe.

Or you can just ask the list when you have a question!

All the best!
Dave
Sander L. (Guest)
on 2006-04-20 13:19
(Received via mailing list)
> :positive? is a symbol, so it can't be converted into a block:

yes it can, using the extensions gem and require 'extensions/symbol'
http://extensions.rubyforge.org/rdoc/classes/Symbol.html
(or by defining your own Symbol#to_proc method)
This topic is locked and can not be replied to.