Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name

Why is ❨╯°□°❩╯︵┻━┻ with such an encoding used for a method name?

If you really do not understand the sense of the method name, that is a (Japanese-style) facemark. Whereas English facemarks are rotated 90 degrees counter-clockwise and are long in the vertical direction of the actual face, Japanese facemarks are to be read in the direction as is, and are long in the horizontal direction. The author of this is likely to be either a Japanese, or someone who is influenced by Japanese culture like anime.

In this particular case, each character expresses a certain part. From left to right:

  • The right edge of the face
  • The right arm raised
  • ° The right eye
  • The mouth
  • ° The left eye
  • The left edge of the face
  • The left arm raised
  • An imaginary curve expressing the trace of a thrown table
  • ┻━┻ A thrown upside-down table (most likely a chabudai that used to be seen typically in Japanese homes until some decades ago)

Chabudai gaeshi used to happen often at some feudal Japanese homes until some decades ago. The father had the absolute monarchic right at home, and whenever he was frustrated over something, he would flip the chabudai during dinner to show his anger, and the family (especially the mother) had to prepare the dinner again.

chabudai gaeshi

Here are more variations.

Why does Eclipse complain about Feature envy smell in my code?

Reek is telling you that, because you are adding two properties of the same class, the calculation should actually belong in String. When adding string lengths this is nonsense of course, but in your case the code can be simplified by using $& (the complete matched string):

input_text[$&.size..-1]

How does the syntax MODULE::METHODNAME('string') work

The difference is in the Nokogiri example the method is being called with parentheses and a parameter value which identifies it as a method call. Your DOESTHISWORK method takes no parameters but can be called with empty parentheses e.g.

irb(main):028:0> How::DOESTHISWORK()
In How Method
=> nil

If you add a parameter to your method that can also serve to identify it as a method like so:

irb(main):036:0> How::DOESTHISWORK 'some param'

Starting method names with a lowercase letter is good practice but isn't enforced. Something that begins with a capital letter is assumed to be a constant and will be looked up as such, this is why the parentheses or parameter is needed to indicate a method is being referred to. Another example:

irb(main):051:0> def Example
irb(main):052:1> puts "An example!"
irb(main):053:1> end
=> nil
irb(main):054:0> Example
NameError: uninitialized constant Example
from (irb):54
from /Users/mike/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>'
irb(main):055:0> Example()
An example!
=> nil

What does this Lisp code mean?

Their point is just that the range of available identifiers is larger, but the semantic overloading of certain characters makes it easy to write code that looks like it might be doing something weird.

If you take a closer look at the first sample,

(lambda (*<8-]= *<8-[= ) (or *<8-]= *<8-[= ))

and rename the variable *<8-]= to a, and *<8-[= to b, we see that it's a pretty simple function:

(lambda (a b) (or a b))

In the second case, it's just making the point that since things like +, <, and so on aren't special operators or anything, they're just symbols with the names "+" and "<", you can use them as variable names. Again, we can rename variables and turn

(defun :-] (<) (= < 2))

into

(defun :-] (a) (= a 2))

This one is rather unusual, since the colon prefix indicates a keyword (a special sort of self-evaluating constant in Common Lisp), but SBCL seems to handle it:

CL-USER> (defun :-] (<) (= < 2))
:-]
CL-USER> (:-] 2)
T
CL-USER> (:-] 3)
NIL

Putting function definitions on keyword symbols is somewhat unusual, but it's not unheard of, and can be useful. E.g., see Naming functions with keywords.

What kinds of names are legal for Ruby methods?

Q1. But I am not sure what .[] part is doing.

Almost everything is an object in Ruby, and we can define a method on anything that's an object. So []= is a method defined on the Hash class (an object) in Ruby, just like + and - are methods on numbers (also objects):

> 4 + 5
# => 9

> 4.+(5)
# => 9

> 10.-(3)
# => 7

Likewise, we can call the .[] or .[]= methods for objects that define it, like an Array:

> arr = ['hi', 'there', 'welcome', 'to', 'StackOverflow']
> arr.[](3)
# => "to"

or like your Hash:

> hash = {:name => 'Bob', :age => 27}
> hash[:name]
# => "Bob"

> hash[:name] = 'Steve'
# => "Steve"

Again, Ruby lets us put methods on anything that's an object, and (almost) everything is an object in Ruby. So we can define that .[] method on any class we like:

> class Foo
> def [](arg)
> arg
> end
> end
> Foo.new[456]
> # => 456

Since instances of objects are also objects, we can define that method to be only on specific instances:

> h = {}           # a Hash instance
> def h.[](arg) # we're giving this instance a new method
> "received #{arg}"
> end
> h[123]
# => "received 123"

Other instances of the same class won't get that implementation:

> {:foo => :bar}[123]   # other Hash instances don't have this method,
# so they're using the default Hash#[] method
# => nil

.[] is something of a special case in one respect, because we let you skip the parentheses and put the argument right inside the brackets:

> arr[3] == arr.[](3)
# => true

Q2. Can you use [] or other non-alphabets in a Ruby method name?

No, you can't use [] in the name of an arbitrary Ruby method. This is an operator (like + or - in the previous example).

You can only overload specific operators, namely (listed in order of precedence):

  • !, ~, + (unary)
  • **
  • - (unary)
  • *, /, %
  • +, - (binary)
  • <<, >>
  • &
  • |, ^
  • <, <=, =>, >
  • ==, ===, !=, =~, !~, <=>

Otherwise, a Ruby method can contain any mixture of alphanumeric Unicode characters and underscores. (I'm using "alphanumeric" loosely here -- Ruby is very liberal in what it allows, and generally speaking any character that isn't otherwise reserved by the language to tokenize things will work.) It may optionally end with a single ! or ?, and it may not start with a number.

So these are valid method names:

  • present?
  • valid_user?
  • replace!
  • replace
  • 更换 (but you probably should make your method names using the A-Z Latin alphabet so developers don't hate you)
  • ❨╯°□°❩╯︵┻━┻
  • ┬─┬ノ❨º_ºノ❩
  • replace_all

  • REPLACE_1

  • REPLACE_ALL

Note that while the last two are valid method names, by convention Rubyists usually reserve ALL-CAPS identifiers for constants, not methods.

ruby code: why put colon in front of variable name (inside initialize method)

Those are keyword arguments.

You can use them by name and not position. E.g.

ThatClass.new(var1: 42, var2: "foo")

or

ThatClass.new(var2: "foo", var1: 42)

An article about keyword arguments by thoughtbot

Find two consecutive rows

Assuming the rows have sequential IDs, something like this may be what you're looking for:

select top 1 * 
from
Bills b1
inner join Bills b2 on b1.id = b2.id - 1
where
b1.IsEstimate = 1 and b2.IsEstimate = 1
order by
b1.BillDate desc

Combine multiple results in a subquery into a single comma-separated value

1. Create the UDF:

CREATE FUNCTION CombineValues
(
@FK_ID INT -- The foreign key from TableA which is used
-- to fetch corresponding records
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @SomeColumnList VARCHAR(8000);

SELECT @SomeColumnList =
COALESCE(@SomeColumnList + ', ', '') + CAST(SomeColumn AS varchar(20))
FROM TableB C
WHERE C.FK_ID = @FK_ID;

RETURN
(
SELECT @SomeColumnList
)
END

2. Use in subquery:

SELECT ID, Name, dbo.CombineValues(FK_ID) FROM TableA

3. If you are using stored procedure you can do like this:

CREATE PROCEDURE GetCombinedValues
@FK_ID int
As
BEGIN
DECLARE @SomeColumnList VARCHAR(800)
SELECT @SomeColumnList =
COALESCE(@SomeColumnList + ', ', '') + CAST(SomeColumn AS varchar(20))
FROM TableB
WHERE FK_ID = @FK_ID

Select *, @SomeColumnList as SelectedIds
FROM
TableA
WHERE
FK_ID = @FK_ID
END


Related Topics



Leave a reply



Submit