Flowdock

Notes posted to Ruby

RSS feed
June 28, 2012
0 thanks

see also – similar methods

See also Time#strftime and Date#strftime . (They work similarly, but have different APIdock notes.)

June 22, 2012 - (v1_8_6_287 - v1_9_3_125)
3 thanks

Test if an array is included in another

Array

class Array
   def included_in? array
     array.to_set.superset?(self.to_set)
   end
end

[1,2,4].included_in?([1,10,2,34,4]) #=> true
June 11, 2012
0 thanks

more_than? instance method

Over the weekend I kept running into instances where I was writing code like this:

Code example

arr = ['hello', 'world']

if arr.length > 2
 # do stuff
else
 # do something else
end

So I ended up extending the core and adding an instance method of more_than?

Code example

class Array
  def more_than?(num)
    length > num
  end
end

Usage

arr = ['hello', 'world']
puts "Hello" if arr.more_than? 1
May 26, 2012 - (v1_9_3_125)
0 thanks

rindex with identically array elements

Code Example

a = [1,1,1]
a.rindex( a.min ) #=> 2
May 17, 2012
0 thanks

Starts with capital letter alternative

Just adding an anchor to the regular expression seems simpler (and was faster in my benchmarks, not that that matters much):

'Abracadabra' =~ /^[A-Z]/
May 15, 2012
0 thanks

railz

heyyyy

May 13, 2012
2 thanks

Undefined Method `mktmpdir' for Dir:Class

Be sure to

require 'tmpdir'

before using it. Read more at http://mikbe.tk/2011/03/07/temporary-directory.

May 3, 2012
0 thanks

Starts with a Capital Letter

(or any regular expression you’d like)

'Abracadabra'[0..0] =~ /[A-Z]/       # => true
April 26, 2012 - (v1_9_3_125)
0 thanks

Dir

The documentation is using File.directory?(“/path/to/directory”), but the method being referred to is Dir.exists?().

Dir.exists?(".")
 => true

The source code is the same as File.directory?().

April 24, 2012 - (v1_9_3_125)
0 thanks

BasicObject.new

Instantiates a new blank object (devoid of methods). The only class method of class BasicObject. see “ri BasicObject”

eg,

> o=BasicObject.new (Object doesn’t support #inspect)

>

> o.methods NoMethodError: undefined method `methods’ for #<BasicObject:0x0000000267a0a0>

> def o.to_s > self > end

> nil

> o

> #<BasicObject:0x0000000267a0a0>

April 9, 2012
This note might be spam Show
March 18, 2012
0 thanks

Use Join to Turn Array Items into a String.

If you’re looking to take an array like

[ 'don', 'draper' ]

And get

'don draper'

Then use join instead:

[ 'don', 'draper' ].join( ' ' ) 

#=> 'don draper'
March 18, 2012
0 thanks

Destructive to the Original String.

Just as an FYI this function is destructive to the original String object.

name = 'draper' #=> "draper"

name.insert( 0, 'don ' ) #=> 'don draper'

name #=> 'don draper'
February 23, 2012
0 thanks

Be careful with path vs. endpoint

URI.join uses a delimiter – forward slash (/) – to decide if joined strings are a path or endpoint. In order to include strings as part of the path, they must end with a forward slash (/). Otherwise, they are assumed to be an endpoint and are overritten by your new “endpoint”.

Used this way, it (kind of) makes sense:

1.9.2p290 :021 > URI.join("http://localhost/test","main.json")
 => #<URI::HTTP:0x007fa68e81c270 URL:http://localhost/main.json> 

1.9.2p290 :022 > URI.join("http://localhost/test/","main.json")
 => #<URI::HTTP:0x007fa68e80e0d0 URL:http://localhost/test/main.json> 

It is especially confusing when you pass 3 strings and the 3rd (your endpoint) overwrites the 2nd (which you expected to be part of the path).

1.9.2p290 :023 > URI.join("http://localhost/", "test", "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

1.9.2p290 :024 > URI.join("http://localhost/", "test/", "main.json")
 => #<URI::HTTP:0x007fa68ce14c60 URL:http://localhost/test/main.json> 

Now, consider that you are probably using a variable for the string value of ‘test’.

1.9.2p290 :025 > controller = 'test'
1.9.2p290 :026 > URI.join("http://localhost/", controller, "main.json")
 => #<URI::HTTP:0x007fa68cec0ba0 URL:http://localhost/main.json> 

Your `controller` is simply ignored. Or rather, your endpoint(?) was overwritten.

I’m not sure what versions of ruby this affects. As you can see I am using 1.9.2p290.

February 12, 2012 - (v1_8_6_287 - v1_9_2_180)
2 thanks

More Examples

Code

class User < Struct.new(:name, :age, :gender) 
end

user = User.new("Matz", 43, "male")