Does Boo support DateTime literals? - boo

Does Boo support DateTime literals? Something like this:
myDate = #2011/1/1#
There is no mention about it on the Boo wiki: http://docs.codehaus.org/display/BOO/Builtin+Literals. Thanks.

Per documentation, it appears that it doesn't.

Related

How is it that you can do `Integer('1')` in Ruby?

How does the standard library allow you to coerce a String into an Integer with Integer('1') #=> 1? Is there a language feature that allows one to create this kind of conversion syntax in a class?
It's not a class, just a method in disguise.
We can do this -
'1'.to_i => 1

Does ruby have an identity function, i.e. x.fn == x, for all x?

Sometimes when I'm passing a method name as an argument, I find myself wishing for an identity function such that any_obj.send(:identity) == any_obj, so instead of this:
(transform.nil?) ? my_obj : my_obj.send(transform)
I could just write
my_obj.send(transform || :identity)
(This is a bit of a contrived example -- an identity function could do more than save a few keystrokes in more complicated examples.)
It would be easy enough to open up the definition of Object and add it, but is there something already there that I've overlooked? Does anyone else want this as well?
P.S.: I know my example should really be stated any_obj.send(:identity).equal?(any_obj), but sometimes pedantry obscures the question.
Yes. You're looking for Object#itself, which is available in Ruby 2.2+.
From the docs:
itself → an_object
Returns obj.
string = 'my string' #=> "my string"
string.itself.object_id == string.object_id #=> true
Your question is related to this one. It seems that now, Ruby is going to have #itself method.
There is not any for now, but Matz has expressed an opinion favoring such method to be implemented: https://bugs.ruby-lang.org/issues/6373. It is likely to be implemented in future versions of Ruby.

Comma separated string to symbols

Do you know any elegant way to achieve this in ruby plz ?
string = 'title,url'
#expected final variable => :title,:url or 'title','url'
I've looked in the API documentation (both ruby and rails) and didn't find any simple method, before writing any loop I wanted to be sure I wasn't missing something.
Thx !
Try this:
string.split(",").map &:to_sym # => [:title, :url]

What is the favorable naming convention for methods or properties returning a boolean value in Ruby?

I've seen all of these:
is_valid
is_valid?
valid?
Is there a preferred one?
EDIT: More conditionals:
has_comment has_comment? comment?
was_full was_full? full?
Please do add more descriptive examples.
I think the convention is mostly to add a '?' at the end of the method instead of 'is'
valid?
In favor of trying the code to be 'natural language' like, is_valid? should be most suitable for me. Lets show an example:
if #order.is_valid?
#order.save
end

How can ruby do this task (Case-insensitive string search & replace in Ruby)?

I have some problem with replace string in Ruby.
My Original string : What the human does is not like what animal does.
I want to replace to: ==What== the human does is not like ==what== animal does.
I face the problem of case sensitive when using gsub. (eg. What , what) I want to keep original text.
any solution?
If I understood you correctly this is what you want to do:
puts "What the human does is not like what animal does.".gsub(/(what)/i, '==\1==')
which will output
==What== the human does is not like ==what== animal does.
another version without brackets () in regex,
puts "What the human does is not like what animal does.".gsub(/what/i,'==\0==')
==What== the human does is not like ==what== animal does.
The important thing to take account of in all 3 answers so far, is the use of the "i" modifier on the regular expression. This is the shorthand way to specify the use of the Regexp::IGNORECASE option.
A useful Ruby Regexp tutorial is here and the class is documented here
Use the block form of gsub.
"What the human does is not like what animal does.".gsub(/(what)/i) { |s| "==#{s}==" }
=> "==What== the human does is not like ==what== animal does."

Resources