mentby.com
Blog | Jobs | Help | Signup | Login

loading

ruby split not working properly

Tue, 26 Feb 2013 20:36:48 -0800 Post Comments

Try

   something.split('\\')

The backslash is used to escape special characters.  In your case to
encode a single quote in the string rather than terminating the string
literal.

IRB is behaving funny because it thinks the string is incomplete, it is
looking for a closing single quote.

The double-backslash encodes as a single backslash in the string.

Gary Wright

Enumerator usage

Mon, 21 Jan 2013 16:42:45 -0800 Post Comments

I don't think it is stressed enough that a block is a syntacticstructure
that is *always* part of a method call.

When a method executes, the block associated with the method call can
be executed implicitly via the 'yield' keyword, or explicitly reified
into a Proc object.  Once captured within an Proc object it can beexecuted
via the #call or #[] methods on the object (which are instance methodsof
the Proc class).

The reification (or capture) can be triggered via the block argument in
a formal argument list:

    def foo(arg0, &block)
      yield("arg to the block)    # call implicit block
      block.call("arg to block")    # call same block via Proc#callinstance method
      block["arg to block"]         # call same block via Proc#[]instance method
    end

or the reification can be triggered on demand:

    def foo(arg0)
      if some_condition)
            captured = Proc.new        # Proc.new withoutexplicit block captures current implicit block
          else
            yield("arg to block")  # call implicit block
          end
        end

Proc objects can also be created via the Kernel#proc method call:

    addition =         proc { |a,b| a   b }
    multiplication =   proc { |c,d| c * d }

    puts addition.call(1,2)         # => 3
    puts multiplication.call(2,10)  # => 20

In these examples, the blocks are syntactically part of the calls to tothe proc method.

    puts addition.class             # => Proc
    puts multiplication.class       # => Proc

Gary Wrigh

Proc.new v. lambda

Wed, 07 Mar 2012 18:54:13 -0800 Post Comments

It is always hard to answer 'why?' with respect to design decisions.

I don't have any special insight but Ruby tends towards shallowinheritance graphs and 'wide' interfaces.  Instead of Collection, Array,Stack, Queue, Dequeue, FixedArray, VariableArray, etc., there is justArray with 88 methods (ruby 1.9.3-p125).

Similarly instead of Proc and Lambda, there is just Proc.

I guess the most accurate, but perhaps unsatisfying answer, regardingthe design of core Ruby classes is that they are that way because thatis what Matz likes. :-)

Gary Wrigh
Group(s)
Profile Widget
Copy and paste this HTML code to your blog or website: