Notes posted to Ruby
RSS feedsee also – similar methods
See also Time#strftime and Date#strftime . (They work similarly, but have different APIdock notes.)
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
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
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]/
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.
Starts with a Capital Letter
(or any regular expression you’d like)
'Abracadabra'[0..0] =~ /[A-Z]/ # => true
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>
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'
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'
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.
More Examples
Code
class User < Struct.new(:name, :age, :gender) end user = User.new("Matz", 43, "male")