Method chaining on a new line in boo - boo

Is it possible to make method chaining in a new line, like you can do in C#?
var foo = bar
.MethodOne()
.MethodTwo()

whitespace is not significant inside () so the following is legal boo code:
a = (bar
.Foo()
.Bar())

You should use '\' symbol. See sample:
a = 123 \
.ToString() \
.Length
print a

Related

Add comment to line in multiline %w in ruby

I have a multiline string array via percent string like this:
array = %w(test
foo
bar)
I want to add a comment message to the foo entry, something like
array = %w(test
# TODO: Remove this line after fix #1
foo
bar)
Is there any way to do it without converting it to basic array like this?
array = ['test',
# TODO: Remove this line after fix #1
'foo',
'bar']
I think there is no way to make that work, because %w() evaluates every space delimited element inside it to string.
There's no way from inside the string to make Ruby evaluate that string.
The only and tricky way:
array = %W(test
##foo
bar).reject(&:empty?)
Note capital W and reject

Ruby method help required

I am reading Metaprogramming Ruby book, and there is method, which I cant understant:
def to_alphanumeric(s)
s.gsub /[^\w\s]/, ''
end
I see there is Argument Variable (s), which is called lately and is converted to some weird expression?
What exactly can I do with this method, is he useful?
Following method works just fine:
def to_alphanumeric(s)
s.gsub %r([aeiou]), '<\1>'
end
p = to_alphanumeric("hello")
p p
>> "h<>ll<>"
But if I upgrade method to class, simply calling the method + argv to_alphanumeric, no longer work:
class String
def to_alphanumeric(s)
s.gsub %r([aeiou]), '<\1>'
end
end
p = to_alphanumeric("hello")
p p
undefined method `to_alphanumeric' for String:Class (NoMethodError)
Would it hurt to check the documentation?
http://www.ruby-doc.org/core-2.0/String.html#method-i-gsub
Returns a copy of str with the all occurrences of pattern substituted for the second argument.
The /[^\w\s]/ pattern means "everything that is not a word or whitespace"
Take a look at Rubular, the regular expression /[^\w\s]/ matches special characters like ^, /, or $ which are neither word characters (\w) or whitespace (\s). Therefore the function removes special characters like ^, / or $.
>> "^/$%hel1241lo".gsub /[^\w\s]/, ''
=> "hel1241lo"
call it simple like a function:
>> to_alphanumeric("U.S.A!")
=> "USA"

Concise way of prefixing string if prefix is not empty

Is there a shorter way of doing the following?
foo =
config.include?(:bar) ?
"#{bar}.baz" :
"baz"
I'm looking for a readable one-liner that appends a variable, plus a delimiter, if the variable exists (assuming it's a string).
config is a Hash.
You could do this:
foo = [bar, 'baz'].compact.join('.')
If bar is nil then compact will remove it from the array and delimiter won't be added.
foo = "#{"bar." if config.include?(:bar)}baz"

Using regexes in Ruby

I have a regex, that I'm trying to use in Ruby. Here is my Regex, and it works in Java when I add the double escape keys
\(\*(.*?)\*\)
I know this is a simple question, but how would I write this as a ruby expression and set it equal to a variable? I appreciate any help.
try this:
myregex = /\(\*(.*?)\*\)/
To be clear, this is just to save the regex to a variable. To use it:
"(**)" =~ myregex
Regular expressions are a native type in Ruby (the actual class is "Pattern"). You can just write:
mypat = /\(\*(.*?)\*\)/
[Looks like anything between '(' / ')' pairs, yes?]
You can then do
m = mypat.match(str)
comment = m[1]
...or, more compactly
comment = mypat.match(str)[1]
try this:
if /\(\*(.*?)\*\)/ === "(*hello*)"
content = $1 # => "hello"
end
http://rubular.com/r/7eCuPX3ri0

ruby whitespace

are there different sensitivities/settings to whitespace in ruby?
i have a RoR project, where an active record call has a lot of components:
max_stuff = FooSummary.select("max(stuff) as stuff")
.joins(:foo => :bar)
.where("user_id = ? and record_date < ?", r.user_id, r.record_date)
.group("user_id")
.first
1.9.3 works fine with this on my mac, but on the ubuntu server it runs on, it complains about the fact that .joins is on a separate line (unexpected . expecting kEND)
what gives?
This syntax was introduced in Ruby 1.9.1:
Language core changes
New syntax and semantics
…
Newlines allowed before ternary colon operator (:) and method call dot operator (.)
Most likely your server is running an older Ruby version, i.e. 1.9.0 or 1.8.x.
Move the period to the preceding line. If parsing line-by-line,
foo = bar
looks like a full statement, and the next line, taken separately, is a syntax error:
.baz
However, this can't be a statement:
foo = bar.
and the parser knows it has to append the next line as well:
baz
(which gives the same parse as foo = bar.baz, as expected).
Maybe
max_stuff = FooSummary.select("max(stuff) as stuff") \
.joins(:foo => :bar) \
.where("user_id = ? and record_date < ?", r.user_id, r.record_date) \
.group("user_id") \
.first
You can also try various combinations of the following, where either ( ends the line, or ) begins the line. Here I am showing both, and you can adjust to your liking.
max_stuff = FooSummary.select(
"max(stuff) as stuff"
).joins(
:foo => :bar
).where(
"user_id = ? and record_date < ?", r.user_id, r.record_date
).group(
"user_id"
).first
Put dottes on the end of lines
max_stuff = FooSummary.select("max(stuff) as stuff").
joins(:foo => :bar).
where("user_id = ? and record_date < ?", r.user_id, r.record_date).
group("user_id").
first

Resources